Skip to content

Commit db96c45

Browse files
committed
feat: warn about conflicts between state and module
1 parent 3a5e530 commit db96c45

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/store.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,13 @@ function installModule (store, rootState, path, module, hot) {
282282
const parentState = getNestedState(rootState, path.slice(0, -1))
283283
const moduleName = path[path.length - 1]
284284
store._withCommit(() => {
285+
if (process.env.NODE_ENV !== 'production') {
286+
if (moduleName in parentState) {
287+
console.warn(
288+
`[vuex] state field "${moduleName}" was overridden by a module with the same name`
289+
)
290+
}
291+
}
285292
Vue.set(parentState, moduleName, module.state)
286293
})
287294
}

test/unit/modules.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,24 @@ 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+
state () {
554+
return { value: 1 }
555+
},
556+
modules: {
557+
value: {
558+
state: () => 2
559+
}
560+
}
561+
})
562+
expect(store.state.value).toBe(2)
563+
expect(console.warn).toHaveBeenCalledWith(
564+
`[vuex] state field "value" was overridden by a module with the same name`
565+
)
566+
})
567+
550568
it('dispatching multiple actions in different modules', done => {
551569
const store = new Vuex.Store({
552570
modules: {

0 commit comments

Comments
 (0)