3

I have two stores on my Nuxt.js app and I need to dispatch an action to another store.

export const actions = {
   addToCart({ state, commit, dispatch }) {
    dispatch('CartLoadingStore/enableLoadingBar')

    this.$axios
      .post('something')
      .then(response => {
        (...)
        dispatch('CartLoadingStore/disableLoadingBar')
      })
  },
}

It seems to me like I cannot dispatch an action to a different store. Is that right? Or is there a way to do so?

The above will result in the error:

[vuex] unknown local action type: CartLoadingStore/enableLoadingBar, global type: StoreTheActionDispatchedFrom/CartLoadingStore/enableLoadingBar

1 Answer 1

15

You need to add root param to your dispatch call

dispatch('CartLoadingStore/disableLoadingBar', null, { root: true })

Here docs

2
  • Thanks Aldarund. Is there a documentation for this? Couldn't find anything in the nuxt docs..
    – user9801251
    Jan 21, 2019 at 9:32
  • @mauxtin i updated answer with link to docs. Since its vuex its stated in docs for vuex
    – Aldarund
    Jan 21, 2019 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.