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

Julien Le Coupanec picture Julien Le Coupanec · Jun 18, 2017 · Viewed 18.4k times · Source

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?

Answer

Julien Le Coupanec picture Julien Le Coupanec · Jun 18, 2017

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 })