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

vue.jsVuejs2Vuex

vue.js Problem Overview


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?

vue.js Solutions


Solution 1 - vue.js

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJulien Le CoupanecView Question on Stackoverflow
Solution 1 - vue.jsJulien Le CoupanecView Answer on Stackoverflow