45

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?

Julien Le Coupanec
  • 6,324
  • 3
  • 43
  • 56

1 Answers1

109

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 })
dwp4ge
  • 1,694
  • 20
  • 24
Julien Le Coupanec
  • 6,324
  • 3
  • 43
  • 56
  • 11
    just want to add something : if global has " namespaced: true," you have to do : commit('global/globalMutation', payload, { root: true }) – Softmixt Oct 09 '18 at 19:45
  • More info here: https://vuex.vuejs.org/api/#commit ( { root: true} works also for dispatch btw. ) – Larzan Apr 02 '20 at 01:16