Thuta Learning
Vue.js
BasicWeb DevelopmentBeginner

Computed Properties and Watchers

Vue.jsLesson 620 min

What you'll walk away with

  • Build a computed value
  • Know when to use watch versus computed

Explain it simply

Computed Properties and Watchers, in plain language

computed() is a derived value whose result is cached until its dependencies change. Use watch() for side effects like API calls, local storage, or logging. Instead of writing complicated calculations in the template, use computed.

Problem → Why → Solution

The problem — why this concept matters

Making an API call inside a computed getter

A common mistake

Using watch to duplicate state for a simple derived value

Core concept — understand how it works

Learn when to reach for computed() for derived state and watch() for side effects.

Mental model
  1. reactive source
  2. dependency tracking
  3. updated value or UI

Build a computed value

Core syntax or pattern

vue
<script setup>
import { ref, computed, watch } from 'vue'

const price = ref(12000)
const quantity = ref(2)
const total = computed(() => price.value * quantity.value)

watch(quantity, (next, previous) => {
  console.log(`Quantity: ${previous} → ${next}`)
})
</script>

<template><strong>Total: {{ total }} MMK</strong></template>

Learn when to reach for computed() for derived state and watch() for side effects.

Read the code step by step

  1. reactive source<script setup>

    computed() is a derived value whose result is cached until its dependencies change. Use watch() for side effects like API calls, local storage, or logging. Instead of writing complicated calculations in the template, use computed.

  2. dependency trackingwatch(quantity, (next, previous) => {

    Learn when to reach for computed() for derived state and watch() for side effects.

  3. updated value or UI<template><strong>Total: {{ total }} MMK</strong></te...

    Apply Computed Properties and Watchers in a small example and test normal, edge, and failure cases.

Execution flow
  1. reactive source
  2. dependency tracking
  3. updated value or UI

Success

Build a computed value

Failure

Making an API call inside a computed getter

Let's break it down simply

computed() is a derived value whose result is cached until its dependencies change. Use watch() for side effects like API calls, local storage, or logging. Instead of writing complicated calculations in the template, use computed.

vue
<script setup>
import { ref, computed, watch } from 'vue'

const price = ref(12000)
const quantity = ref(2)
const total = computed(() => price.value * quantity.value)

watch(quantity, (next, previous) => {
  console.log(`Quantity: ${previous} → ${next}`)
})
</script>

<template><strong>Total: {{ total }} MMK</strong></template>
You should see
Total: 24000 MMK

Try it yourself

Calculate the average score from a score list using computed. Log a message with watch every time a score changes.

Computed Properties — Vue.js

Real-world usage

How to apply it in a real project

Apply Computed Properties and Watchers in a small example and test normal, edge, and failure cases.

Mini challenge

Build it yourself

Calculate the average score from a score list using computed. Log a message with watch every time a score changes.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
vue
<script setup>
import { ref, computed, watch } from 'vue'

const price = ref(12000)
const quantity = ref(2)
const total = computed(() => price.value * quantity.value)

watch(quantity, (next, previous) => {
  console.log(`Quantity: ${previous} → ${next}`)
})
</script>

<template><strong>Total: {{ total }} MMK</strong></template>

Expected: Build a computed value

Show hint

Apply Computed Properties and Watchers in a small example and test normal, edge, and failure cases.

Quick quiz 1 / 3

What best describes the core idea of Computed Properties and Watchers?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • reactive source → dependency tracking → updated value or UI
  • Learn when to reach for computed() for derived state and watch() for side effects.
  • Making an API call inside a computed getter
  • Apply Computed Properties and Watchers in a small example and test normal, edge, and failure cases.

Computed Properties — Vue.js

Easy traps

  • Making an API call inside a computed getter
  • Using watch to duplicate state for a simple derived value

Exercise

Calculate the average score from a score list using computed. Log a message with watch every time a score changes.

You'll know it worked when: Total: 24000 MMK

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Computed Properties and Watchers | Thuta Learning