Skip to content

Commit 538ee58

Browse files
simplesmilerktsn
authored andcommitted
fix: Warn about conflicts between state and module (#1365)
* feat: warn about conflicts between state and module * fix: show module path when it overwrite a state field
1 parent e5ca2d5 commit 538ee58

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/store.js

+7
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,13 @@ function installModule (store, rootState, path, module, hot) {
315315
const parentState = getNestedState(rootState, path.slice(0, -1))
316316
const moduleName = path[path.length - 1]
317317
store._withCommit(() => {
318+
if (process.env.NODE_ENV !== 'production') {
319+
if (moduleName in parentState) {
320+
console.warn(
321+
`[vuex] state field "${moduleName}" was overridden by a module with the same name at "${path.join('.')}"`
322+
)
323+
}
324+
}
318325
Vue.set(parentState, moduleName, module.state)
319326
})
320327
}

test/unit/modules.spec.js

+22
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,28 @@ describe('Modules', () => {
547547
store.dispatch('parent/test')
548548
})
549549

550+
it('module: warn when module overrides state', () => {
551+
spyOn(console, 'warn')
552+
const store = new Vuex.Store({
553+
modules: {
554+
foo: {
555+
state () {
556+
return { value: 1 }
557+
},
558+
modules: {
559+
value: {
560+
state: () => 2
561+
}
562+
}
563+
}
564+
}
565+
})
566+
expect(store.state.foo.value).toBe(2)
567+
expect(console.warn).toHaveBeenCalledWith(
568+
`[vuex] state field "value" was overridden by a module with the same name at "foo.value"`
569+
)
570+
})
571+
550572
it('dispatching multiple actions in different modules', done => {
551573
const store = new Vuex.Store({
552574
modules: {

0 commit comments

Comments
 (0)