VueJS 2.0

How to Use Computed Properties in VueJS

How to Use Computed Properties in VueJS
Written by Yogesh Koli

What is Computed Properties in VueJS?

Computed properties in vuejs is the replacement of inline logic on template, we often use to write simple operation, filters on template it self, but for live project it’s hard to maintain such kind of logic on templates, so vuejs provides a better way to replace doing inline coding by using computed properties.

In last tutorial How to Handle Events in VueJS 2.0 I have given details on how to use events in vuejs and to demonstrate event handling I have given a simple example by creating to do list application, I am going to use same example here to explain the use of computed properties, if you have’t read the tutorials do visit and a to take a look on to do list example.

Before going to follow real time example first let’s take a look at use and the basic syntax of vuejs

How to Use Computed Properties in VueJS?

Here we are going to use simple example to get understand the syntax of vuejs computed properties:

Let’s say we an name and we want to display it revers we can do that in following way:

Example Without using Computed Properties:

Output: hsegoY

So in the above example we are simply using the name and converting into array next by using reverse method we simply doing reverse of the array elements and join it again to string by using job method, to the reason to giving details on logic is we are writing little more complicated logic on template, let’s replace by using computed properties:



Example using Computed Properties:

Output: hsegoY

The output is same for both the examples but the way we are adding our logic is different and it makes to use write javascript in side the script instead of writing it on template.

Real time to do list Example using Computed Properties:

In the following example we have array of tasks and we going to filter those using computed properties, meaning will filter in-completed and completed tasks.

If you take a look in details you will get to know you can use computed properties in real time web applications.

You can checkout the live example of todo list application of above script to see how above script works:  To Do List Live Example Using VueJS Computed Properties

Conclusion of the tutorial:

So in this tutorial we have learn following points:

  1. What is VueJS Computed Properties
  2. How to use Computed properties
  3. How to create to do list application using computed properties.
  4. Filter Array Object using Computed properties

Get Notified when new stuff is available

More intermediate and advanced stuff is coming soon, I will update you when we get new stuff updated hear you can subscribe to get notified whenever we update new tutorial.

Make sure to confirm your email address by clicking on activation link from the verification email to complete subscription.

Stay Tuned.

Happy Learning!

About the author

Yogesh Koli

Yogesh Koli is a software engineer & a Blogger lives in India. He’s driven by an addiction to learning and a love for adventure. he has 5+ years of experience working with the front-end, back-end, web application development, and system design.