Is there a way to dispatch actions between two namespaced vuex modules?

vue.jsVuejs2Vuex

vue.js Problem Overview


Is it possible to dispatch an action between namespaced modules?

E.g. I have vuex modules "gameboard" and "notification". Each are namespaced. I would like to dispatch an action from the gameboard to the notification module.

I thought I could use the module name in the dispatch action name like this:

// store/modules/gameboard.js
const actions = {
    myaction ({dispatch}) {
        ...
        dispatch('notification/triggerSelfDismissingNotifcation', {...})
    }
}

// store/modules/notification.js
const actions = {
    triggerSelfDismissingNotification (context, payload) {
        ...
    }
}

But when I try to do this I get errors that make me thing vuex is trying to dispatch an action within my gameboard module:

> [vuex] unknown local action type: notification/triggerSelfDismissingNotification, global type: gameboard/notification/triggerSelfDismissingNotification

Is there a way of dispatching from vuex module to module or do I need to create some kind of a bridge in the root vuex instance?

vue.js Solutions


Solution 1 - vue.js

You just need to specify that you're dispatching from the root context:

// from the gameboard.js vuex module
dispatch('notification/triggerSelfDismissingNotifcation', {...}, {root:true})

Now when the dispatch reaches the root it will have the correct namespace path to the notifications module (relative to the root instance).

This is assuming you're setting namespaced: true on your vuex store module.

Solution 2 - vue.js

dispatch('mobuleB/actionB', null, { 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
QuestionChris SchmitzView Question on Stackoverflow
Solution 1 - vue.jsJakeView Answer on Stackoverflow
Solution 2 - vue.jsKaddu LivingstoneView Answer on Stackoverflow