is this possible to pass parameter in computed properties in Vue.Js. I can see when having getters/setter using computed, they can take a parameter and assign it to a variable. like here from documentation:
// ... computed: { fullName: { // getter get: function () { return this.firstName + ' ' + this.lastName }, // setter set: function (newValue) { var names = newValue.split(' ') this.firstName = names[0] this.lastName = names[names.length - 1] } } } // ...
Is this also possible:
// ... computed: { fullName: function (salut) { return salut + ' ' + this.firstName + ' ' + this.lastName } } // ...
Where computed property takes a argument and returns desired output. However when I try this, I am getting this error:
vue.common.js:2250 Uncaught TypeError: fullName is not a function(…)
Should I be using methods for such cases?
The computed property is a very powerful feature of Vue. js, and we have learned that it comes in handy when we have to change them when their dependencies get changed. We have learned to pass the parameter and use it in the computed property.
Also should mention that Computed Properties are automatically cached while Methods are not. If you are running an 'expensive' operation, it is best to cache this data as a Computed Property or else the function will re-run everytime the page refreshes which creates unnecessary overhead.
In Vue. js, computed properties enable you to create a property that can be used to modify, manipulate, and display data within your components in a readable and efficient manner. You can use computed properties to calculate and display values based on a value or set of values in the data model.
Computed Caching vs Methods Instead of a computed property, we can define the same function as a method. For the end result, the two approaches are indeed exactly the same. However, the difference is that computed properties are cached based on their reactive dependencies.
In Vue.js, computed properties are properties that are derived from other reactive properties. If we just return an expression in the computed property, then we can access it like any other reactive property. However, we can’t call it like a method to pass in values to computed properties this way.
You can pass parameters but either it is not a vue.js way or the way you are doing is wrong. However there are cases when you need to do so.I am going to show you a simple example passing value to computed property using getter and setter.
The returned value is the one that’s actually printed in the Vue.js template. Show activity on this post. You can pass parameters but either it is not a vue.js way or the way you are doing is wrong.
Note that getters accessed via methods will run each time you call them, and the result is not cached. That is called Method-Style Access and it is documented on the Vue.js docs. Show activity on this post.
Most probably you want to use a method
<span>{{ fullName('Hi') }}</span> methods: { fullName(salut) { return `${salut} ${this.firstName} ${this.lastName}` } }
Technically you can use a computed property with a parameter like this:
computed: { fullName() { return salut => `${salut} ${this.firstName} ${this.lastName}` } }
(Thanks Unirgy
for the base code for this.)
The difference between a computed property and a method is that computed properties are cached and change only when their dependencies change. A method will evaluate every time it's called.
If you need parameters, there are usually no benefits of using a computed property function over a method in such a case. Though it allows you to have a parametrized getter function bound to the Vue instance, you lose caching so not really any gain there, in fact, you may break reactivity (AFAIU). You can read more about this in Vue documentation https://vuejs.org/v2/guide/computed.html#Computed-Caching-vs-Methods
The only useful situation is when you have to use a getter and need to have it parametrized. For instance, this situation happens in Vuex. In Vuex it's the only way to synchronously get parametrized result from the store (actions are async). Thus this approach is listed by official Vuex documentation for its getters https://vuex.vuejs.org/guide/getters.html#method-style-access
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With