Croll
Croll

Reputation: 3791

Ho do you call Vuex module Action from Vue component action?

Let's say i have Vuex store module storeModuleName, and i want to invoke its action from some component.

Component:


export default {
  actions: {
   close() {
     dispatch('storeModuleName/storeModuleAction');
   },
  }
...

Error:

dispatch is not defined

Upvotes: 0

Views: 510

Answers (1)

Moumen Soliman
Moumen Soliman

Reputation: 1674

You need mapActions, mapActions let you connect with actions inside modules

import { mapActions } from "vuex";
export default {
  methods: {
    ...mapActions({
      storeModuleActionName: "storeModuleName/storeModuleAction"
    })
  },
  mounted(){
    this.storeModuleActionName();
  }
}

And then you can use storeModuleActionName as a normal method, Check the link about mapActions

Upvotes: 4

Related Questions