如果您将我们应用程序的所有状态都保持在一个大状态中,那么存储会变得非常臃肿。为了解决这个问题,Vuex 允许我们将 store 划分为模块。在这里,每个模块都可以包含自己的状态、突变、动作、getter,甚至是嵌套模块。
让我们以多个模块为例,在vuex中配置它们并访问不同的模块,
const moduleOne = {
state: { ... },
mutations: { ... },
actions: { ... },
getters: { ... }
}
const moduleTwo = {
state: { ... },
mutations: { ... },
actions: { ... },
getters: { ... }
}
const store = new Vuex.Store({
modules: {
one: moduleOne,
two: moduleTwo
}
})
store.state.one // -> `moduleOne's state
store.state.two // -> `moduleTwo's state