1. // main.js
    2. import store from './store'
    3. new Vue({
    4. store,
    5. ...
    6. })
    1. // store/index.js
    2. import Vue from 'vue'
    3. import Vuex from 'vuex'
    4. Vue.use(Vuex)
    5. import common from './commom'
    6. export default new Vuex.Store({
    7. modules: {
    8. common,
    9. },
    10. })
    1. // common.js
    2. const common = {
    3. namespaced: true,
    4. state: {
    5. menuList: null,
    6. },
    7. getters: {
    8. menuList: state => state.menuList,
    9. },
    10. mutations: {
    11. SET_MENU: (state, o) => {
    12. state.menuList = o
    13. },
    14. },
    15. actions: {
    16. UpdateMenu({ commit }, menusList) {
    17. commit('SET_MENU', menusList)
    18. },
    19. },
    20. }
    21. export default common

    如果希望你的模块具有更高的封装度和复用性,你可以通过添加 namespaced: true 的方式使其成为带命名空间的模块。当模块被注册后,它的所有 getter、action 及 mutation 都会自动根据模块注册的路径调整命名。

    • getters[‘account/isAdmin’]
    • dispatch(‘account/login’)
    • commit(‘account/login’)
      • modules可以深层嵌套