Author -  Sai gowtham

Computed Properties in Vue.js with examples

In this tutorial, we are going to learn about computed properties in 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.

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY