mapState doesn't work with non namespaced module
See original GitHub issueVersion
3.1.1
Reproduction link
https://codesandbox.io/s/vuex-issue-with-mapstate-5zb3x
Steps to reproduce
In the reproduction link, there is a non namespaced module with has a count in it’s state and a doubleCount in it’s getters.
In the HelloWorld component I am trying to use vuex helpers to access the state and the getters like:
...mapState(['count']),
...mapGetters(['doubleCount]')
What is expected?
Since the module it’s not namespaced I should be able to access the state and getters of the module with vuex helpers.
Both mapState
and mapGetters
should work fine.
What is actually happening?
However, only the mapGetters helper it’s working.
In order to get the mapState
to work I should do:
...mapState({
count: state => state.myModule.count
})
However IMHO, the mapState should work with just a string since the module in the end it’s merged with the global store.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:16
- Comments:6
Top Results From Across the Web
mapState on non-namespaced module - vue.js - Stack Overflow
The most efficient way with non-namespaced modules is to define a getter. The advantage is that you don't have to redefine it in...
Read more >[Solved]-mapState on non-namespaced module-Vue.js
The most efficient way with non-namespaced modules is to define a getter. The advantage is that you don't have to redefine it in...
Read more >Modules | Vuex
module state is already nested and not affected by namespace option ... When binding a namespaced module to components with the mapState ......
Read more >[vuex] module namespace not found in mapActions() - Laracasts
[vuex] module namespace not found in mapActions() ... Additionally my old getters do no longer work: ... mapState({ checkoutStatus: state => state.cart.
Read more >Refactoring Vuex with Map Helpers and Modules - Vue Mastery
There's no need to change how we're mapping with mapState in this component because we are already mapping to event . Now the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@maeldur While technically you are correct, from user perspective it’s the source of confusion.
I can map state of my namespaced modules like this:
…while for not-namespaced modules I have to use object/arrow functions:
At the very least
mapState()
should throw an error, if an array of state variables is passed to it without a namespace. Right now, it just creates computed variables with undefined values and the user (me in my case) stares blankly at the screen for 15 minutes trying to figure out what they are doing wrong.Since state is not registered to the global scope, IMO mapState should accept the module name for non-namespaced state properties. It seems needlessly verbose when you’re pulling multiple properties from the state from the same module.
versus
when the namespace property of the module doesn’t otherwise effect the access to state.
I also agree that an error or warning should be thrown when trying to use the map helpers for nonexistent properties, instead of secretly creating empty ones.