默认情况,模块的mutations、actions等 里的方法是会直接放到根mutations、根actions里的,这时候如果不同模块出现命名冲突,可能会导致错误。命名空间就相当于在mutations、actions的方法名前面加上了模块名,防止出现命名冲突。
例子:
const store = createStore({
modules: {
account: {
namespaced: true,
// 模块内容(module assets)
state: () => ({ ... }),
getters: {
isAdmin () { ... } // -> getters['account/isAdmin']
},
actions: {
login () { ... } // -> dispatch('account/login')
},
mutations: {
login () { ... } // -> commit('account/login')
},
}
}
}