Skip to content
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

Warn about conflicts between state and module #1365

Merged
merged 2 commits into from
Nov 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,13 @@ function installModule (store, rootState, path, module, hot) {
const parentState = getNestedState(rootState, path.slice(0, -1))
const moduleName = path[path.length - 1]
store._withCommit(() => {
if (process.env.NODE_ENV !== 'production') {
if (moduleName in parentState) {
console.warn(
`[vuex] state field "${moduleName}" was overridden by a module with the same name`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simplesmiler what do u think about including the module name in the latter part as well? [vuex] state field "${moduleName}" was overridden by a module with the same name ("${moduleName}")

)
}
}
Vue.set(parentState, moduleName, module.state)
})
}
Expand Down
18 changes: 18 additions & 0 deletions test/unit/modules.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,24 @@ describe('Modules', () => {
store.dispatch('parent/test')
})

it('module: warn when module overrides state', () => {
spyOn(console, 'warn')
const store = new Vuex.Store({
state () {
return { value: 1 }
},
modules: {
value: {
state: () => 2
}
}
})
expect(store.state.value).toBe(2)
expect(console.warn).toHaveBeenCalledWith(
`[vuex] state field "value" was overridden by a module with the same name`
)
})

it('dispatching multiple actions in different modules', done => {
const store = new Vuex.Store({
modules: {
Expand Down