const moduleA = {
state: {....},
mutations: {....},
actions: {....},
getters: {....}
}
const moduleB = {
state: {....},
mutations: {....},
actions: {....},
getters: {....}
}
const store = new Vuex.Store({
modules: {
a: moduleA,
b: moduleB
}
})
store.state.a // moduleA的状态
store.state.b // moduleB的状态
const moduleA = {
state: { count: 0},
mutations: {
increment (state) {
// state是模块的局部状态,也就是上面的state
state.count++
}
},
getters: {
doubleCount (state, getters, rootState) {
// 参数 state为当前局部状态,rootState为根节点状态
return state.count * 2
}
},
actions: {
incremtnIfOddRootSum ( { state, commit, rootState } ) {
// 参数 state为当前局部状态,rootState为根节点状态
if ((state.cont + rootState.count) % 2 === 1) {
commit('increment')
}
}
}
}
const store = new Vuex.Store({
modules: {
account: {
namespaced: true,
state: {...}, // 模块内的状态已经是嵌套的,namespaced不会有影响
getters: { // 每一条注释为调用方法
isAdmin () { ... } // getters['account/isAdmin']
},
actions: {
login () {...} // dispatch('account/login')
},
mutations: {
login () {...} // commit('account/login')
},
modules: { // 继承父模块的命名空间
myPage : {
state: {...},
getters: {
profile () {...} // getters['account/profile']
}
},
posts: { // 进一步嵌套命名空间
namespaced: true,
getters: {
popular () {...} // getters['account/posts/popular']
}
}
}
}
}
})
modules: {
foo: {
namespaced: true,
getters: {
// 在这个被命名的模块里,getters被局部化了
// 你可以使用getter的第四个参数来调用 'rootGetters'
someGetter (state, getters, rootSate, rootGetters) {
getters.someOtherGetter // -> 局部的getter, ‘foo/someOtherGetter'
rootGetters.someOtherGetter // -> 全局getter, 'someOtherGetter'
}
},
actions: {
// 在这个模块里,dispatch和commit也被局部化了
// 他们可以接受root属性以访问跟dispatch和commit
smoeActino ({dispatch, commit, getters, rootGetters }) {
getters.someGetter // 'foo/someGetter'
rootGetters.someGetter // 'someGetter'
dispatch('someOtherAction') // 'foo/someOtherAction'
dispatch('someOtherAction', null, {root: true}) // => ‘someOtherAction'
commit('someMutation') // 'foo/someMutation'
commit('someMutation', null, { root: true }) // someMutation
}
}
}
}
computed: {
...mapState({
a: state => state.some.nested.module.a,
b: state => state.some.nested.module.b
}),
methods: {
...mapActions([
'some/nested/module/foo',
'some/nested/module/bar'
])
}
}
computed: {
...mapStates('some/nested/module', {
a: state => state.a,
b: state => state.b
})
},
methods: {
...mapActions('some/nested/module',[
'foo',
'bar'
])
}
const MyModule = {
state () {
return {
foo: 'far'
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有