ရိုးရိုးလေး ရှင်းပြမယ်
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() ကို မှန်ကန်စွာအသုံးပြုမည်။
- Reactive source
- Dependency tracking
- Updated value / UI
Computed value တည်ဆောက်ရန်
အဓိက Syntax / 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>Derived state အတွက် computed() နှင့် side effect များအတွက် watch() ကို မှန်ကန်စွာအသုံးပြုမည်။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Reactive source
<script setup>computed() သည် dependency မပြောင်းသရွေ့ result ကို cache လုပ်ထားသော derived value ဖြစ်သည်။ watch() ကို API call, local storage သို့မဟုတ် logging ကဲ့သို့ side effect များအတွက်သုံးသည်။ Template ထဲတွင်ရှုပ်ထွေးသောတွက်ချက်မှုရေးမည့်အစား computed သုံးပါ။
- Dependency tracking
watch(quantity, (next, previous) => {Derived state အတွက် computed() နှင့် side effect များအတွက် watch() ကို မှန်ကန်စွာအသုံးပြုမည်။
- Updated value / UI
<template><strong>Total: {{ total }} MMK</strong></te...Computed Properties and Watchers ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။
- Reactive source
- Dependency tracking
- 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 သုံးပါ။
<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 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 ကိုခန့်မှန်းပါ
<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
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- 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