Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a shared helper function from within a mutation or action in Vuex

Tags:

vue.js

vuex

I am trying to separate out some code that is common among many calls in my Vuex mutations. I am getting the feeling that this is discouraged but I don't understand why.

Have a look at an image of some sample code below:

I have added this 'helpers' entry in the Vuex - this obviously doesn't exist but how can I call the shared helper function 'getColumn' from mutations and/or actions?

enter image description here


Or do I have resort to calling a static method on a 'VuexHelper' class? :(

Something like:

enter image description here

Note I have already looked at the following:

  1. Vue Mixins - yes, something like that could work but is not supported in Vuex - also, vue methods don't return a value...
  2. I have looked at Modules but these still don't give me what I need, i.e. a simple re-usable function that returns a value.

Thanks

like image 521
Marcel Avatar asked Aug 19 '18 08:08

Marcel


People also ask

How do you access getters in mutations Vuex?

To access getters within Vuex mutations with Vue. js, we just use the state in the getter and call the getter with the state . //... const store = new Vuex.

How do I dispatch action from mutation Vuex?

Mutations can't dispatch further actions, but actions can dispatch other actions. So one option is to have an action commit the mutation then trigger the filter action. Another option, if possible, would be to have all filters be getters that just naturally react to data changes like a computed property would.

What is difference between mutation and action in Vuex?

Mutations are intended to receive input only via their payload and to not produce side effects elsewhere. While actions get a full context to work with, mutations only have the state and the payload .

Which method of a Vuex store can be used to call an action?

In Vuex, actions are functions that call mutations. Actions exist because mutations must be synchronous, whereas actions can be asynchronous. You can define actions by passing a POJO as the actions property to the Vuex store constructor as shown below. To "call" an action, you should use the Store#dispatch() function.


2 Answers

I don't see why you may want to put the helper function within the store. You can just use a plain function.

function getColumn(state, colName) {
  // Do your thing.
}

const vstore = new Vuex.Store({
  // ....
  mutations: {
    removeColumn(state, colName) {
      var column = getColumns(state, colName);
    }
  }
};

On the other hand, if you really need that, you can access the raw module and all that's included:

var column = this._modules.root._rawModule.helpers.getColumns(state, colName);

Although this syntax is not documented and can change for later versions.

like image 188
dereli Avatar answered Oct 15 '22 21:10

dereli


You can implement your Vuex getter as a method-style getter. This lets you pass in the specific column as an argument:

getters: {
  getColumn: state => colName => {
    return state.columns[colName] || null
  }
}

Then getColumn can be used within the store like so:

let column = getters.getColumn('colNameString')

vuex docs > getters > method style access

like image 20
Gal Weissman Avatar answered Oct 15 '22 23:10

Gal Weissman