-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
module namespace not found in mapState() when using store.registerModule() and non-namespaced module #855
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is expected. Because namespace value and state path can be different, you need specify For example: const store = new Vuex.Store({
modules: {
foo: {
namespaced: false,
modules: {
bar: {
namespaced: true,
state: { value: 'Hello' }
}
}
}
}
})
new Vue({
el: '#app',
store,
computed: mapState('bar', ['value'])
}) |
@ktsn Why do the other map* functions work as expected then? Looks like a bug to me. The documentation should clarify that mapState only works with namespaced dynamic modules. Sorry I do not have the time to send a pull request. |
Other helpers also should not work without |
They work if you remove the namespace argument from mapGetters: https://jsfiddle.net/awvv3g60/4/ The idea from my point of view is that by registering a non-namespaced module, you want to be able to access state, mutations, etc. as if they were in the root store. So it makes sense that mapGetters works if you do not pass any namespace argument. The question is, why doesn't mapState work the same way? It's confusing to me, and, if intended (I still can't see why) it should be documented as a gotcha. To clarify: mapState only works with namespaced modules. Other map* functions work with both. |
The map* helpers works as same way - if you pass namespace value, they find corresponding assets from that namespace, otherwise from root namespace. Only difference is the state is registered as tree structure while the other things are registered in root namespace in default. |
Would it be possible to write something in the module docs to REINFORCE that to use map* helpers with subModules you need to namespace them. I mean, I've spent a couple of time trying to make an mapping mixin to work, and thought that I was doing something really wrong, until I realized that maybe someone had hit the same rock as I in the issues board. Well, good to know now. I'll inform that namespaced modules are "mandatory" to use the mixin. |
Just if anyone comes accross this, to use mapState with non-namespaced Vuex modules you can use the callback variant of mapState: ...mapState({
firstItem: (state) => state.firstItem, // Uses the root state
secondItem: (state) => state.module.secondItem, // Uses the state of the module
}), Shoutouts to this dude here on stackoverflow for his hint 👍 |
Version
2.3.0
Reproduction link
https://jsfiddle.net/awvv3g60/2/
Steps to reproduce
See fiddle. Notice console error.
What is expected?
mapState to be able to load the state from a dynamically registered, non-namespaced module.
What is actually happening?
Got an error:
This is just bug report #812 reopened with a working JSFiddle link.
The bug is in mapState only, mapGetters, mapActions and mapMutations all work as expected.
Calling mapState without passing a namespace as argument (i.e. mapState(['name']), still does not work. As such, mapState only works with namespaced module.
The text was updated successfully, but these errors were encountered: