Skip to content
Advertisement

Is it possible to use Vuex mapActions inside an exported module

Is it possible to call Vuex mapActions from a separate module that’s imported into a component?

I’m trying to standardize a set of functions in a vue.js web app. I’d like to import them into each component and pass some values the to function operate. I’m using vuex to manage state. Currently each component calls these functions each time they’re loaded (identically the same).

I’d like to refactor into this into one module and import it into each component as needed. This code uses mapActions as part of it’s function. Below are the relevant pieces of code: component, module, vuex action

Vue component:

JavaScript

the module code (advance.js):

JavaScript

the vuex code:

JavaScript

when the component runs the imported function i get:

JavaScript

when i remove the this from this.currentComponent i get:

JavaScript

thanks in advance for any guidance.

Advertisement

Answer

mapActions is a shortcut for creating a method that looks something like this

JavaScript

When you call this function, the this context is timer. Since timer does not have a $store property, you get the error Cannot read property 'dispatch' of undefined. The quickest way to get around this would be to change the this context to the component which does have a $store property. You could do this by passing the component as a third property in updatePage and binding currentComponent to the function.

JavaScript

I’d recommend using a mixin for this type of behavior though.

JavaScript

In your component, import the timerMixin and register it as a mixin. Those methods would then be available directly on your component and you can call them with a small tweak to your existing code.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement