大中型項目中使用vuex進行狀態管理時,經常會按模塊分割到不同的module中去,而操作中難免有模塊中的active相互調用的情況,然而有時也會出現一些問題,這里順便記錄下
store目錄結構
在userLoginOut中調用其他模塊的active
const actions = { // login userLoginOut({ commit, dispatch, rootGetters }, userInfo) { commit('permission/SET_ROUTES', [], { root: true }) // 清空permissin下SET_ROUTES console.log(rootGetters['user/usermenu']) // 獲取user 模塊下的usermenu屬性 dispatch('tagsView/delAllViews', null, { root: true }) // 派發事件執行tagsView 下的delAllViews active } }
上面方法中需要注意的是{root:true}
參數,如commit('permission/SET_ROUTES', [])
這樣vuex 就會默認拼接當前module user
作為key 去查找對應的mutation,於是就會有如下錯誤
vuex.esm.js?52c1:663 [vuex] unknown local mutation type: permission/SET_ROUTES, global type: user/permission/SET_ROUTES
在此記錄一下希望可以幫助遇到該問題的朋友