Thuta Learning
Angular
BasicWeb Developmentအလယ်အလတ်

Lifecycle Hooks

Angularသင်ခန်းစာ 620 မိနစ်

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

  • Lifecycle Hooks ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
  • နမူနာ UI ကို run၊ state/output ခန့်မှန်းပြီး edge/error cases စမ်းနိုင်ရန်
  • လက်တွေ့ project တွင် typed safe pattern နဲ့ common pitfalls ကိုခွဲသိနိုင်ရန်

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

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 သုံးရပါတယ်။

Mental Model
  1. Input + state
  2. Component + template
  3. Rendered Angular UI

Lifecycle Hooks ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

typescript
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 ကို အဆင့်လိုက်ဖတ်မယ်

  1. Input + stateimport { Component, OnInit, OnDestroy, OnChanges } fr...

    Lifecycle Hooks များသည် component ၏ creation နှင့် destruction အဆင့်များတွင် code များ run နိုင်ရန် Angular မှ ပေးထားသော methods များဖြစ်သည်။

  2. Component + templatengOnChanges(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 သုံးရပါတယ်။

  3. Rendered Angular UI}

    Parent က input ကိုနှစ်ကြိမ်ပြောင်းသော child တစ်ခုရေးပြီး hook order ကို log လုပ်ပါ။ Interval တစ်ခုထည့်ပြီး navigate away လုပ်ချိန် cleanup ဖြစ်ကြောင်းစစ်ပါ။

Execution Flow
  1. Input + state
  2. Component + template
  3. 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 များဖြစ်သည်။

typescript
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
  }
}
You should see
(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 ကိုခန့်မှန်းပါ
typescript
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

Lifecycle Hooks ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

ဘယ်အမှားကို အရင်ဆုံးရှောင်သင့်လဲ။

Quick Quiz 3 / 3

Real project မှာ ဘယ်လိုအသုံးချသင့်လဲ။

ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ

  • 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

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

  • `ngAfterViewChecked` လို မကြာခဏ run သော hook ထဲ heavy work သို့ state update ထည့်ခြင်းက performance နဲ့ change-detection loop ပြဿနာဖြစ်စေနိုင်ပါတယ်။
  • Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။

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

Lifecycle Hooks နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: (Component lifecycle အဆင့်တိုင်းတွင် console messages များ ပေါ်လာမည်)

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Lifecycle Hooks | Thuta Learning