Thuta Learning
Vue.js
BasicWeb Developmentအခြေခံ

Computed Properties and Watchers

Vue.jsသင်ခန်းစာ 620 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Computed value တည်ဆောက်ရန်
  • watch နှင့် computed အသုံးပြုရမည့်အချိန် ခွဲခြားရန်

ရိုးရိုးလေး ရှင်းပြမယ်

Computed Properties and Watchers ကို ရိုးရိုးလေးနားလည်မယ်

computed() သည် dependency မပြောင်းသရွေ့ result ကို cache လုပ်ထားသော derived value ဖြစ်သည်။ watch() ကို API call, local storage သို့မဟုတ် logging ကဲ့သို့ side effect များအတွက်သုံးသည်။ Template ထဲတွင်ရှုပ်ထွေးသောတွက်ချက်မှုရေးမည့်အစား computed သုံးပါ။

Problem → Why → Solution

ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ

computed getter ထဲတွင် API call ပြုလုပ်ခြင်း

ဖြစ်လေ့ရှိတဲ့အမှား

ရိုးရိုး derived value အတွက် watch ဖြင့် state ထပ်တူကူးခြင်း

Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်

Derived state အတွက် computed() နှင့် side effect များအတွက် watch() ကို မှန်ကန်စွာအသုံးပြုမည်။

Mental Model
  1. Reactive source
  2. Dependency tracking
  3. Updated value / UI

Computed value တည်ဆောက်ရန်

အဓိက Syntax / 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>

Derived state အတွက် computed() နှင့် side effect များအတွက် watch() ကို မှန်ကန်စွာအသုံးပြုမည်။

Code ကို အဆင့်လိုက်ဖတ်မယ်

  1. Reactive source<script setup>

    computed() သည် dependency မပြောင်းသရွေ့ result ကို cache လုပ်ထားသော derived value ဖြစ်သည်။ watch() ကို API call, local storage သို့မဟုတ် logging ကဲ့သို့ side effect များအတွက်သုံးသည်။ Template ထဲတွင်ရှုပ်ထွေးသောတွက်ချက်မှုရေးမည့်အစား computed သုံးပါ။

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

    Derived state အတွက် computed() နှင့် side effect များအတွက် watch() ကို မှန်ကန်စွာအသုံးပြုမည်။

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

    Computed Properties and Watchers ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။

Execution Flow
  1. Reactive source
  2. Dependency tracking
  3. Updated value / UI

Success

Computed value တည်ဆောက်ရန်

Failure

computed getter ထဲတွင် API call ပြုလုပ်ခြင်း

ရိုးရိုးလေး ရှင်းပြမယ်

computed() သည် dependency မပြောင်းသရွေ့ result ကို cache လုပ်ထားသော derived value ဖြစ်သည်။ watch() ကို API call, local storage သို့မဟုတ် logging ကဲ့သို့ side effect များအတွက်သုံးသည်။ Template ထဲတွင်ရှုပ်ထွေးသောတွက်ချက်မှုရေးမည့်အစား 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

လက်တွေ့စမ်းကြည့်ရန်

Score list တစ်ခုမှ average score ကို computed ဖြင့်တွက်ပါ။ Score ပြောင်းတိုင်း watch ဖြင့် log ထုတ်ပါ။

Computed Properties — Vue.js

Real-world usage

လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ

Computed Properties and Watchers ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။

Mini challenge

ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ

Score list တစ်ခုမှ average score ကို computed ဖြင့်တွက်ပါ။ Score ပြောင်းတိုင်း watch ဖြင့် log ထုတ်ပါ။

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
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>

ရလဒ်: Computed value တည်ဆောက်ရန်

အရိပ်အမြွက် ကြည့်ရန်

Computed Properties and Watchers ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။

Quick Quiz 1 / 3

Computed Properties and Watchers ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

ဘယ်အမှားကို အရင်ဆုံးရှောင်သင့်လဲ။

Quick Quiz 3 / 3

Real project မှာ ဘယ်လိုအသုံးချသင့်လဲ။

ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ

  • Reactive source → Dependency tracking → Updated value / UI
  • Derived state အတွက် computed() နှင့် side effect များအတွက် watch() ကို မှန်ကန်စွာအသုံးပြုမည်။
  • computed getter ထဲတွင် API call ပြုလုပ်ခြင်း
  • Computed Properties and Watchers ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။

Computed Properties — Vue.js

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • computed getter ထဲတွင် API call ပြုလုပ်ခြင်း
  • ရိုးရိုး derived value အတွက် watch ဖြင့် state ထပ်တူကူးခြင်း

လေ့ကျင့်ခန်း

Score list တစ်ခုမှ average score ကို computed ဖြင့်တွက်ပါ။ Score ပြောင်းတိုင်း watch ဖြင့် log ထုတ်ပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: Total: 24000 MMK

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Computed Properties and Watchers | Thuta Learning