ရိုးရိုးလေး ရှင်းပြမယ်
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