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.
- reactive source
- dependency tracking
- updated value or UI
Build a computed value
Core syntax or pattern
<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
- 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.
- dependency tracking
watch(quantity, (next, previous) => {Learn when to reach for computed() for derived state and watch() for side effects.
- 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.
- reactive source
- dependency tracking
- 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.
<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>Total: 24000 MMKTry 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
<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
Quick quiz 2 / 3
Quick quiz 3 / 3
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