Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use vuex namespaced getters with arguments?

I have issue when Im using vuex.

I have getters in namespaced module and I cant figurę out how to get the data with Ii when Im passing some arguments.

this.$store.getters.feeders.getFeedersById(id)

And in maper.

...mapGetters({
   feeders: ['feeders/getFeedersById'](this.id)

Getting error like this getter is not a function. What else shoudl I do?

like image 479
Canor Avatar asked Jan 23 '18 11:01

Canor


People also ask

What is Namespaced in Vuex?

In the previous Vuex tutorial, we learned that by default, getters, actions, and mutations inside modules are registered under the global namespace, which allows multiple modules to react to the same mutation or action.

Are the results of a Vuex getter cached?

Vuex allows us to define "getters" in the store. You can think of them as computed properties for stores. As of Vue 3.0, the getter's result is not cached as the computed property does.


2 Answers

feedersById(state) {
    return rowId => {
        if (state.feedersArray.hasOwnProperty(rowId)) {
            return state.feedersArray[rowId].map(id => state.feeders[id]);
        }

    }
},

feedersId() {
  if (this.rowData) {
   return this.$store.getters['feeders/feedersById'](this.rowData.ac_id);
  }
}

Okey I had some mistake there and now it works properly. Thanks!! :)

like image 188
Canor Avatar answered Oct 09 '22 01:10

Canor


You can also declare a getter function like this:

feedersById: (state) => rowID => {
  if (state.feedersArray.hasOwnProperty(rowId)) {
    return state.feedersArray[rowId].map(id => state.feeders[id]);
  }
}
like image 34
gbragamonte Avatar answered Oct 09 '22 02:10

gbragamonte