Vue Computed Properties

Computed properties are like data properties, except they depend on other properties.

Computed properties are written like methods, but they do not accept any input arguments.

Computed properties are updated automatically when a dependency changes, while methods are called on when something happens, like with event handling for example.

Computed properties are used when outputting something that depends on something else.

Computed Properties are Dynamic

The big advantage with a computed property is that it is dynamic, meaning it changes depending on for example the value of one or more data properties.

Computed properties is the third configuration option in the Vue instance that we will learn. The first two configuration options we have already looked at are 'data' and 'methods'.

As with 'data' and 'methods' computed properties also has a reserved name in the Vue instance: 'computed'.

Syntax

const app = Vue.createApp({
  data() {
    ...
  },
  computed: {
    ...
  },
  methods: {
    ...
  }
})

Computed Property 'yes' or 'no'

Let's say we want a form to create items in a shopping list, and we want to mark if a new item is important or not. We could add a 'true' or 'false' feedback when the checkbox gets checked, like we have done in an example before:

Example

An input element is made dynamic so that the text reflects the status.

<input type="checkbox" v-model="chbxVal"> {{ chbxVal }}
data() {
  return {
    chbxVal: false
  }
}
Try it Yourself »

However, if you you ask someone if something is important, they will most likely answer 'yes' or 'no' instead of 'true' or 'false'. So to make our form more fitting with normal language (more intuitive) we should have 'yes' or 'no' as feedback on the checkbox instead of 'true' or 'false'.

And guess what, a computed property is a perfect tool to help us with that.

Example

With a computed property 'isImportant' we can now customize the text feedback to the user when the checkbox is toggled on and off.

<input type="checkbox" v-model="chbxVal"> {{ isImportant }}
data() {
  return {
    chbxVal: false
  }
},
computed: {
  isImportant() {
    if(this.chbxVal){
      return 'yes'
    }
    else {
      return 'no'
  }
}
Try it Yourself »

Computed Properties vs. Methods

Computed properties and methods are both written as functions, but they are different:

  • Methods runs when called from HTML, but computed properties updates automatically when a dependency change.
  • Computed properties are used the same way we use data properties, but they are dynamic.

Vue Exercises

Test Yourself With Exercises

Exercise:

Provide the correct code so that the 'isImportant' computed property is shown on the screen.

<div id="app">
  <form>
    <p>
      Important item?
      <label>
         <input type="checkbox" v-model="chbxVal"> 
         
      </label>
    </p>
  </form>
</div>

<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script>
  const app = Vue.createApp({
    data() {
      return {
        chbxVal: false
      }
    },
    : {
      isImportant() {
        if(this.chbxVal){
          return 'yes'
        }
        else {
          return 'no'
        }
      }
    }
  })
 app.mount('#app')
</script>

Start the Exercise



Copyright 1999-2023 by Refsnes Data. All Rights Reserved.