Search code examples
vue.jsvuexvuex-modules

does vuex mapState always require module name as argument even if namespaced:false


I was expecting that if I have a module that has namespaced:false (which I think is also the default) then its state should be part of the "global" or "root" namespace and that I would be able to do " ...mapState('someStateAttribute') and then refer to someStateAttribute but it would seem that I must use ...mapState('modulename','someStateAttribute') or else it does not work.

Is this just the case? or am I missing something?


Solution

  • Have a look at mapState's signature:

    mapState(namespace?: string, map: Array<string> | Object<string | function>): Object
    

    This means the first argument, namespace, is optional. But if you provide a string as first argument, it will be the namespace.

    To achieve what you want you should do:

    ...mapState(['someStateAttribute'])