Need help with uibuilder, Vuex, and Map Helpers

I have run into a roadblock or maybe just confusion on my part when it comes to Vuex coming from uibuilder.

Within my index.js file, I have a store. Then I have the app section that (partially) looks like this:

const app = new Vue({
    el: '#app',
    store,
    components: {
    },
    data: {
    }, // --- End of data --- //
    computed: {
        doneTodos(){
            return  this.$store.getters.doneTodos
        }
    }, // --- End of computed --- //

This works very well (please ignore the non-sensical name). In basically every tutorial I have found on Vuex, they suggest that once you have a lot of computed items, this syntax gets cumbersome. I expect in the end to have a large number and so want to follow the advice. A sample explanation is here at Intro to the vuex map helpers.

By using the mapGetters helper, then my computed section could become:

const app = new Vue({
    el: '#app',
    store,
    components: {
    },
    data: {
    }, // --- End of data --- //
    computed: {
    ...mapGetters({
        doneTodos: 'doneTodos'
        }
    }, // --- End of computed --- //

How to use the helpers is clear enough. The stumbling block is to use each of the helpers, they all say to add:

import { mapGetters } from 'vuex'
or
import { mapState } from 'vuex'
etc.

When I try to add this to my index.js file, then in the console of my browser, I get the following error:
image
Searching the web, it initially looked like the solution was to change the inclusion of vuex from

    <script src="../uibuilder/vendor/vuex/dist/vuex.js"></script>

to

    <script type="module" src="../uibuilder/vendor/vuex/dist/vuex.js"></script>

This doesn't work. I am the limits of my understanding of Javascript, Vue, VueX and Google-Fu.
Any suggestions on how I can enable the use of the Vuex Map Helpers?

lose the import and use

...Vuex.mapGetters(

Source:

2 Likes

Thank you. I can't begin to explain how long I looked for this answer. I am curious how you knew this or what search you used to find it. The stackoverflow question and answer couldn't have been more on the nose and yet I did not come across it during my searches.

yea .. with google searches you have to think like a computer and search for keywords :wink:
so i searched for vuex mapgetters cdn
i added the cdn keyword since you used a script tag to load vuex (even if you load it locally)
because i wanted to get those example codes .. and bingo .. first result

1 Like

Including cdn is so obvious in retrospect, but I never thought to include it in my search. Thanks again.

Glad this got sorted. I've never had the chance to use VueX myself, it is somewhere on the long list of things to do :grinning:

Of course, the problem you hit is that to use an import statement in the browser, you need to introduce a build step which would translate everything for you and build a suitable library resource to load. (Noting that the error msg you got refers to modules which are the next-gen feature of HTML in the browser that will bring features such as imports).

Turns out for my use case (a control system for smart home with many nested components that interact with each other), VueX and the ability to have one store for state instead of passing things up and down the tree of nested components should be invaluable.

Just one more piece to leverage the value of uibuilder which is huge.

1 Like

:grinning:

Hopefully then, you will like the v5 release!

Better layout for the config in the Editor, more error checking in the editor, better structure for the ExpressJS routes, lots of bugs squashed.

Though one thing to start thinking about - v5 will, I'm afraid, need a reinstall of your front-end libraries via the Editor (thankfully a very easy process). That's because I've moved the location for libraries from the userDir folder (where Node-RED puts its nodes) to the uibRoot folder (the one that contains the folders for your front-end code). While this is a one-off pain, the result is much simpler library management process that will be a lot more robust and lays the foundation for a future update to allow per-uib-instance libraries. It also means that there is a better separation of duties between Node-RED and uibuilder and enables you to install pretty much anything that npm will install including local packages and ones from GitHub. Because you can move the uibRoot folder to wherever you like, this may also help some people with space management.

The v5 code is also a LOT better structured in many places which lays foundations for further improvements.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.