ရိုးရိုးလေး ရှင်းပြမယ်
Lifecycle Hooks ကို ရိုးရိုးလေးနားလည်မယ်
Lifecycle Hooks များသည် component ၏ creation နှင့် destruction အဆင့်များတွင် code များ run နိုင်ရန် Angular မှ ပေးထားသော methods များဖြစ်သည်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
`ngAfterViewChecked` လို မကြာခဏ run သော hook ထဲ heavy work သို့ state update ထည့်ခြင်းက performance နဲ့ change-detection loop ပြဿနာဖြစ်စေနိုင်ပါတယ်။
ဖြစ်လေ့ရှိတဲ့အမှား
Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Lifecycle hooks တွေက Angular က component instance ဖန်တီး၊ inputs ပြောင်း၊ view render နဲ့ destroy လုပ်တဲ့အဆင့်တွေမှာ code run ရန် contract ပေးပါတယ်။ Constructor ကို dependency wiring အတွက်ထားပြီး input-dependent initialization ကို `ngOnInit`/input change handling မှာလုပ်ပါ။ Subscriptions, timers နဲ့ observers များကို destroy ချိန် cleanup လုပ်ရန် framework-provided destroy utilities သို့ explicit teardown သုံးရပါတယ်။
- Input + state
- Component + template
- Rendered Angular UI
Lifecycle Hooks ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
import { Component, OnInit, OnDestroy, OnChanges } from '@angular/core';
export class MyComponent implements OnInit, OnDestroy, OnChanges {
// Component instance created လုပ်ပြီး properties initialized ဖြစ်သောအခါ
constructor() {
console.log('Constructor called');
}
// Component initialized ဖြစ်ပြီး @Input properties set ပြီးသောအခါ
ngOnInit() {
console.log('Component initialized');
// API calls များ ဒီမှာလုပ်သင့်သည်
}
// @Input properties ပြောင်းလဲသောအခါတိုင်း
ngOnChanges(changes: SimpleChanges) {
console.log('Input changed:', changes);
}
// View initialized ဖြစ်ပြီးသောအခါ
ngAfterViewInit() {
console.log('View ready');
}
// Component destroy လုပ်မည့်အချိန်
ngOnDestroy() {
console.log('Component destroyed');
// Cleanup: unsubscribe, clear timers
}
}Lifecycle hooks တွေက Angular က component instance ဖန်တီး၊ inputs ပြောင်း၊ view render နဲ့ destroy လုပ်တဲ့အဆင့်တွေမှာ code run ရန် contract ပေးပါတယ်။ Constructor ကို dependency wiring အတွက်ထားပြီး input-dependent initialization ကို `ngOnInit`/input change handling မှာလုပ်ပါ။ Subscriptions, timers နဲ့ observers များကို destroy ချိန် cleanup လုပ်ရန် framework-provided destroy utilities သို့ explicit teardown သုံးရပါတယ်။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Input + state
import { Component, OnInit, OnDestroy, OnChanges } fr...Lifecycle Hooks များသည် component ၏ creation နှင့် destruction အဆင့်များတွင် code များ run နိုင်ရန် Angular မှ ပေးထားသော methods များဖြစ်သည်။
- Component + template
ngOnChanges(changes: SimpleChanges) {Lifecycle hooks တွေက Angular က component instance ဖန်တီး၊ inputs ပြောင်း၊ view render နဲ့ destroy လုပ်တဲ့အဆင့်တွေမှာ code run ရန် contract ပေးပါတယ်။ Constructor ကို dependency wiring အတွက်ထားပြီး input-dependent initialization ကို `ngOnInit`/input change handling မှာလုပ်ပါ။ Subscriptions, timers နဲ့ observers များကို destroy ချိန် cleanup လုပ်ရန် framework-provided destroy utilities သို့ explicit teardown သုံးရပါတယ်။
- Rendered Angular UI
}Parent က input ကိုနှစ်ကြိမ်ပြောင်းသော child တစ်ခုရေးပြီး hook order ကို log လုပ်ပါ။ Interval တစ်ခုထည့်ပြီး navigate away လုပ်ချိန် cleanup ဖြစ်ကြောင်းစစ်ပါ။
- Input + state
- Component + template
- Rendered Angular UI
Success
Lifecycle Hooks ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
Failure
`ngAfterViewChecked` လို မကြာခဏ run သော hook ထဲ heavy work သို့ state update ထည့်ခြင်းက performance နဲ့ change-detection loop ပြဿနာဖြစ်စေနိုင်ပါတယ်။
Lifecycle Hooks များသည် component ၏ creation နှင့် destruction အဆင့်များတွင် code များ run နိုင်ရန် Angular မှ ပေးထားသော methods များဖြစ်သည်။
import { Component, OnInit, OnDestroy, OnChanges } from '@angular/core';
export class MyComponent implements OnInit, OnDestroy, OnChanges {
// Component instance created လုပ်ပြီး properties initialized ဖြစ်သောအခါ
constructor() {
console.log('Constructor called');
}
// Component initialized ဖြစ်ပြီး @Input properties set ပြီးသောအခါ
ngOnInit() {
console.log('Component initialized');
// API calls များ ဒီမှာလုပ်သင့်သည်
}
// @Input properties ပြောင်းလဲသောအခါတိုင်း
ngOnChanges(changes: SimpleChanges) {
console.log('Input changed:', changes);
}
// View initialized ဖြစ်ပြီးသောအခါ
ngAfterViewInit() {
console.log('View ready');
}
// Component destroy လုပ်မည့်အချိန်
ngOnDestroy() {
console.log('Component destroyed');
// Cleanup: unsubscribe, clear timers
}
}(Component lifecycle အဆင့်တိုင်းတွင် console messages များ ပေါ်လာမည်)Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Parent က input ကိုနှစ်ကြိမ်ပြောင်းသော child တစ်ခုရေးပြီး hook order ကို log လုပ်ပါ။ Interval တစ်ခုထည့်ပြီး navigate away လုပ်ချိန် cleanup ဖြစ်ကြောင်းစစ်ပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Lifecycle Hooks နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
import { Component, OnInit, OnDestroy, OnChanges } from '@angular/core';
export class MyComponent implements OnInit, OnDestroy, OnChanges {
// Component instance created လုပ်ပြီး properties initialized ဖြစ်သောအခါ
constructor() {
console.log('Constructor called');
}
// Component initialized ဖြစ်ပြီး @Input properties set ပြီးသောအခါ
ngOnInit() {
console.log('Component initialized');
// API calls များ ဒီမှာလုပ်သင့်သည်
}
// @Input properties ပြောင်းလဲသောအခါတိုင်း
ngOnChanges(changes: SimpleChanges) {
console.log('Input changed:', changes);
}
// View initialized ဖြစ်ပြီးသောအခါ
ngAfterViewInit() {
console.log('View ready');
}
// Component destroy လုပ်မည့်အချိန်
ngOnDestroy() {
console.log('Component destroyed');
// Cleanup: unsubscribe, clear timers
}
}ရလဒ်: Lifecycle Hooks ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Parent က input ကိုနှစ်ကြိမ်ပြောင်းသော child တစ်ခုရေးပြီး hook order ကို log လုပ်ပါ။ Interval တစ်ခုထည့်ပြီး navigate away လုပ်ချိန် cleanup ဖြစ်ကြောင်းစစ်ပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Input + state → Component + template → Rendered Angular UI
- Lifecycle hooks တွေက Angular က component instance ဖန်တီး၊ inputs ပြောင်း၊ view render နဲ့ destroy လုပ်တဲ့အဆင့်တွေမှာ code run ရန် contract ပေးပါတယ်။ Constructor ကို dependency wiring အတွက်ထားပြီး input-dependent initialization ကို `ngOnInit`/input change handling မှာလုပ်ပါ။ Subscriptions, timers နဲ့ observers များကို destroy ချိန် cleanup လုပ်ရန် framework-provided destroy utilities သို့ explicit teardown သုံးရပါတယ်။
- `ngAfterViewChecked` လို မကြာခဏ run သော hook ထဲ heavy work သို့ state update ထည့်ခြင်းက performance နဲ့ change-detection loop ပြဿနာဖြစ်စေနိုင်ပါတယ်။
- Parent က input ကိုနှစ်ကြိမ်ပြောင်းသော child တစ်ခုရေးပြီး hook order ကို log လုပ်ပါ။ Interval တစ်ခုထည့်ပြီး navigate away လုပ်ချိန် cleanup ဖြစ်ကြောင်းစစ်ပါ။
Angular — Components — Angular