Vuex | How to commit a global mutation in a module action?

為{幸葍}努か 提交于 2020-03-17 07:45:27

问题


I have an action in a namespaced module and a global mutation (i.e. not in a module). I would like to be able to commit the global mutation inside the action.

// Global mutation
export default {
  globalMutation (state, payload) {
    ...
  }
}

// Action in a namespaced module
export default {
  namespaced: true,

  actions: {
    namespacedAction ({ commit, dispatch, state }, payload) {
      commit({ type: 'globalMutation' })
    }
  }
}

When the namespaced action is dispatched, Vuex displays:

[vuex] unknown local mutation type: globalMutation, global type: module/globalMutation

Is there an option I can pass to the commit function to call this global mutation?


回答1:


Looks like I just found a way with the { root: true } parameter.

commit('globalMutation', payload, { root: true })

If module is namespaced, use global path instead:

commit('module/mutation', payload, { root: true })


来源:https://stackoverflow.com/questions/44618440/vuex-how-to-commit-a-global-mutation-in-a-module-action

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!