Sometimes, we want to add a Vue Vuex getter with argument.
In this article, we’ll look at how to add a Vue Vuex getter with argument.
How to add a Vue Vuex getter with argument?
To add a Vue Vuex getter with argument, we can add a getter function that returns a function that takes the arguments.
For instance, we write
new Vuex.Store({
getters: {
someMethod: (state) => (id) => {
return state.things.find((thing) => thing.id === id);
},
},
});
to create a Vuex store with the someMethod
getter that returns a function that takes the id
parameter.
Then we call someMethod
with the id
we want.
Conclusion
To add a Vue Vuex getter with argument, we can add a getter function that returns a function that takes the arguments.