// main.jsimport store from './store'new Vue({store,...})
// store/index.jsimport Vue from 'vue'import Vuex from 'vuex'Vue.use(Vuex)import common from './commom'export default new Vuex.Store({modules: {common,},})
// common.jsconst common = {namespaced: true,state: {menuList: null,},getters: {menuList: state => state.menuList,},mutations: {SET_MENU: (state, o) => {state.menuList = o},},actions: {UpdateMenu({ commit }, menusList) {commit('SET_MENU', menusList)},},}export default common
如果希望你的模块具有更高的封装度和复用性,你可以通过添加 namespaced: true 的方式使其成为带命名空间的模块。当模块被注册后,它的所有 getter、action 及 mutation 都会自动根据模块注册的路径调整命名。
- getters[‘account/isAdmin’]
- dispatch(‘account/login’)
- commit(‘account/login’)
- modules可以深层嵌套
