Thuta Learning
ရှာဖွေရန်
BasicWeb Developmentbeginner

Computed Properties and Watchers

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

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

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

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

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 PropertiesVue.js

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

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

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

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

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

Computed Properties and Watchers | Thuta Learning