Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I pass parameters in computed properties in Vue.Js

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?

like image 377
Saurabh Avatar asked Nov 10 '16 08:11

Saurabh


People also ask

Can Vue computed properties take parameters?

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.

What is the difference between a computed property and methods in VUE JS?

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.

How do I use computed property in Vue?

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.

What is the main difference between a method and a computed value in VUE JS?

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.

What is a computed property in Vue JS?

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.

Can you pass parameters in Vue JS?

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.

What is the returned value of the Vue JS template?

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.

Are getters accessed via methods cached in Vue JS?

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.


1 Answers

Most probably you want to use a method

<span>{{ fullName('Hi') }}</span>  methods: {   fullName(salut) {       return `${salut} ${this.firstName} ${this.lastName}`   } } 

Longer explanation

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

like image 185
damienix Avatar answered Oct 04 '22 16:10

damienix