Computed Properties in Vue.js with examples
In this post we will give you information about Computed Properties in Vue.js with examples. Hear we will give you detail about Computed Properties in Vue.js with examplesAnd how to use it also give you demo for it if it is necessary.
we are going to learn about computed propertiesin Vue with the help of examples.
What is a Computed Property?
Computed properties just look like data properties in Vue but we can perform some arithmetic and non-arithmetic tasks.
<template> <ul> <li>First name : {{firstName}}</li> <li>Last name : {{lastName}}</li> <li>Full name : {{firstName + ' '+ lastName}}</li> </ul></template><script> data:function(){ return{ firstName: "Sai", lastName: "Gowtham" } }</script>
In the above code, we have created two data properties firstName and lastName and interpolated it inside the template.
If you look into our template we added our Full Namelogic inside the {{}} curly braces in such cases computed properties shine.
Example
Let’s see an example of how to create our first computed property.
Computed properties are declared inside the computed property object.
<template> <ul> <li>First name : {{firstName}}</li> <li>Last name : {{lastName}}</li> <!-- computed property --> <li>Full name : {{fullName}}</li> </ul></template><script>export default{ data:function(){ return{ firstName: "Sai", lastName: "Gowtham" } }, computed:{ fullName:function(){ return this.firstName+' '+this.lastName } }}
Here we added a computed property called fullName which is a function where it returns the full name of the user.
We can use computed properties inside our template just like data properties.
Computed properties are cached by the vue so that it only revaluates the logic if its underlying data property changes it means if firstName or lastName doesn’t change then it just returns the previously computed result without running the function again.
Hope this code and post will helped you for implement Computed Properties in Vue.js with examples. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs