Skip to content

Commit 47cf447

Browse files
ktsnyyx990803
authored andcommitted
avoid to get wrong state with mapState (#709)
* avoid to get wrong state with mapState * checking only namespaced option is enough
1 parent a7bf0ec commit 47cf447

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/store.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ function installModule (store, rootState, path, module, hot) {
226226
const namespace = store._modules.getNamespace(path)
227227

228228
// register in namespace map
229-
if (namespace) {
229+
if (module.namespaced) {
230230
store._modulesNamespaceMap[namespace] = module
231231
}
232232

test/unit/helpers.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,32 @@ describe('Helpers', () => {
6868
expect(vm.a).toBe(7)
6969
})
7070

71+
// #708
72+
it('mapState (with namespace and a nested module)', () => {
73+
const store = new Vuex.Store({
74+
modules: {
75+
foo: {
76+
namespaced: true,
77+
state: { a: 1 },
78+
modules: {
79+
bar: {
80+
state: { b: 2 }
81+
}
82+
}
83+
}
84+
}
85+
})
86+
const vm = new Vue({
87+
store,
88+
computed: mapState('foo', {
89+
value: state => state
90+
})
91+
})
92+
expect(vm.value.a).toBe(1)
93+
expect(vm.value.bar.b).toBe(2)
94+
expect(vm.value.b).toBeUndefined()
95+
})
96+
7197
it('mapMutations (array)', () => {
7298
const store = new Vuex.Store({
7399
state: { count: 0 },

0 commit comments

Comments
 (0)