Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vuexjs getter with argument

Is there a way to pass parameter into getter of vuex store? Something like:

new Vuex.Store({   getters: {     someMethod(arg){        // return data from store with query on args     }   } }) 

So that in component I could use

<template>     <div>         <p>{{someMethod(this.id)}}</p>     </div> </template> <script lang="ts">     import { mapGetters } from "vuex"      export default {         props: ['id'],         computed: mapGetters(['someMethod'])         }     } </script> 

but in vuex first argument is state and second is other getters. Is it possible?

like image 380
Sly Avatar asked Jan 06 '17 10:01

Sly


People also ask

What are mapGetters?

mapGetters and mapActions are basically a helper provided by vuex which returns an object with keys as method names and values as methods with some defined definition. This object when combined with ... (Object spread operator) spreads it out into individual functions in the computed or methods object respectively.

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.

How do I access the Vue getters?

In any case you can call console. log(this. $store) to debug the Store. If you do so you will see the getters are prefixed with the namespace in their name.


2 Answers

One way to do this can be:

new Vuex.Store({   getters: {     someMethod(state){       var self = this;        return function (args) {           // return data from store with query on args and self as this        };            }   } }) 

However, getter does not take arguments and why is explained in this thread:

the naming convention is slightly confusing, getters indicates state can be retrieved in any form, but in fact they are reducers.

Perhaps we should have reducers being pure methods. Which can be used for filtering, mapping ect.

getters then can be given any context. Similar to computed, but you can now combine computed props to getters in vuex option. Which helps structure of components.

Edit:

A better way to achieve the same thing will be using ES6 arrow as detailed out in the answer of nivram80, using method style getters where you can pass a parameter by returning a function form the getter:

new Vuex.Store({   getters: {     someMethod: (state) => (id) => {         return state.things.find(thing => thing.id === id)       }     };          } }) 
like image 165
Saurabh Avatar answered Oct 02 '22 04:10

Saurabh


An ES6 arrow function would work nicely here too. For example, sake, let's say you're looking for a particular 'thing' in your store.

new Vuex.Store({   getters: {     someMethod: (state) => (id) => {       return state.things.find(thing => thing.id === id)     }   }, }) 

Here is another example via the Vuex documentation

like image 39
nivram80 Avatar answered Oct 02 '22 05:10

nivram80