Categories
Vue Answers

How to add a Vue Vuex getter with argument?

Spread the love

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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.