How to send a parameter to getters in vue.js?

by edmond_brakus , in category: Javascript , 20 days ago

How to send a parameter to getters in vue.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by jasen , 19 days ago

@edmond_brakus 

In Vue.js, you cannot directly pass a parameter to a getter as they are designed to purely retrieve data from the state. However, you can pass a parameter to a getter by creating a custom method that takes the parameter and then returns the result of the getter based on that parameter.


For example, you can create a method in your component that takes a parameter and then retrieves the data using a getter with that parameter. Here is an example:

1
2
3
4
5
methods: {
    getDataByParameter(parameter) {
        return this.$store.getters.getData(parameter);
    }
}


In this example, we have a method called getDataByParameter which takes a parameter and then retrieves the data using a getter called getData with that parameter.


You can then call this method in your template like this:

1
<div>{{ getDataByParameter('exampleParameter') }}</div>


This way, you can effectively pass a parameter to getters in Vue.js by creating a custom method in your component.