ရိုးရိုးလေး ရှင်းပြမယ်
Lifecycle Hooks ကို ရိုးရိုးလေးနားလည်မယ်
Component တစ်ခု create, mount, update နှင့် unmount အဆင့်များကိုဖြတ်သန်းသည်။ DOM ကိုလိုအပ်သော code ကို onMounted တွင် run ပါ။ Timer, event listener နှင့် subscription များကို onUnmounted တွင်ရှင်းရမည်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
Event listener ကို cleanup မလုပ်ခြင်း
ဖြစ်လေ့ရှိတဲ့အမှား
setup အတွင်း DOM element ကို mount မဖြစ်ခင် access လုပ်ခြင်း
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
onMounted, onUpdated နှင့် onUnmounted hooks များဖြင့် component lifecycle အလိုက် code run မည်။
- Props / event
- Component + template
- Rendered Vue UI
Mount နှင့် unmount အချိန်ကိုနားလည်ရန်
အဓိက Syntax / Pattern
<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
const width = ref(0)
const measure = () => { width.value = window.innerWidth }
onMounted(() => {
measure()
window.addEventListener('resize', measure)
})
onUnmounted(() => window.removeEventListener('resize', measure))
</script>
<template><p>Viewport: {{ width }}px</p></template>onMounted, onUpdated နှင့် onUnmounted hooks များဖြင့် component lifecycle အလိုက် code run မည်။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Props / event
<script setup>Component တစ်ခု create, mount, update နှင့် unmount အဆင့်များကိုဖြတ်သန်းသည်။ DOM ကိုလိုအပ်သော code ကို onMounted တွင် run ပါ။ Timer, event listener နှင့် subscription များကို onUnmounted တွင်ရှင်းရမည်။
- Component + template
measure()onMounted, onUpdated နှင့် onUnmounted hooks များဖြင့် component lifecycle အလိုက် code run မည်။
- Rendered Vue UI
<template><p>Viewport: {{ width }}px</p></template>Lifecycle Hooks ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။
- Props / event
- Component + template
- Rendered Vue UI
Success
Mount နှင့် unmount အချိန်ကိုနားလည်ရန်
Failure
Event listener ကို cleanup မလုပ်ခြင်း
ရိုးရိုးလေး ရှင်းပြမယ်
Component တစ်ခု create, mount, update နှင့် unmount အဆင့်များကိုဖြတ်သန်းသည်။ DOM ကိုလိုအပ်သော code ကို onMounted တွင် run ပါ။ Timer, event listener နှင့် subscription များကို onUnmounted တွင်ရှင်းရမည်။
<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
const width = ref(0)
const measure = () => { width.value = window.innerWidth }
onMounted(() => {
measure()
window.addEventListener('resize', measure)
})
onUnmounted(() => window.removeEventListener('resize', measure))
</script>
<template><p>Viewport: {{ width }}px</p></template>Viewport: 1280pxလက်တွေ့စမ်းကြည့်ရန်
တစ်စက္ကန့်တိုင်းတိုးသော timer component ရေးပြီး unmount ဖြစ်လျှင် interval ကိုရှင်းပါ။
Lifecycle Hooks — Vue.js
Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Lifecycle Hooks ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
တစ်စက္ကန့်တိုင်းတိုးသော timer component ရေးပြီး unmount ဖြစ်လျှင် interval ကိုရှင်းပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
const width = ref(0)
const measure = () => { width.value = window.innerWidth }
onMounted(() => {
measure()
window.addEventListener('resize', measure)
})
onUnmounted(() => window.removeEventListener('resize', measure))
</script>
<template><p>Viewport: {{ width }}px</p></template>ရလဒ်: Mount နှင့် unmount အချိန်ကိုနားလည်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Lifecycle Hooks ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Props / event → Component + template → Rendered Vue UI
- onMounted, onUpdated နှင့် onUnmounted hooks များဖြင့် component lifecycle အလိုက် code run မည်။
- Event listener ကို cleanup မလုပ်ခြင်း
- Lifecycle Hooks ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။
Lifecycle Hooks — Vue.js