Angular တွင် ngOnInit ဟုခေါ်သော Hook

Hook ngOnInit သည် component စတင်အလုပ်လုပ်သည့်အခါတွင်အလုပ်လုပ်သည်။ ၎င်း၏လုပ်ဆောင်ချက်မှာ class constructor နှင့်ဆင်တူသော်လည်း ပို၍ရှုပ်ထွေးသော လုပ်ငန်းများကို ဥပမာ server မှဒေတာများကိုဆွဲချခြင်းကဲ့သို့သော လုပ်ငန်းများကို ဤတွင်လုပ်ဆောင်နိုင်သည်။

ဤ hook ကိုမည်သို့အသုံးပြုရမည်ကိုကြည့်ရှုကြပါစို့။ ပထမဦးစွာ ၎င်း၏ interface ကို import လုပ်ရန်လိုအပ်ပါသည်။

app/user/user.component.tsimport { OnInit } from '@angular/core';

ထို့နောက် interface ကို component class ထဲသို့ထည့်သွင်းရန်လိုအပ်ပါသည်။

app/user/user.component.tsexport class UserComponent implements OnInit {

}

ထို့နောက် ကျွန်ုပ်တို့သည် component class ထဲတွင် ngOnInit method ကိုရေးသားနိုင်ပြီး class စတင်အလုပ်လုပ်စဉ်တွင် ဤ method သည်အလိုအလျောက်လုပ်ဆောင်သွားမည်ဖြစ်သည်။ ၎င်း၏လုပ်ဆောင်ချက်ကိုစစ်ဆေးကြည့်ရအောင်။

app/user/user.component.tsexport class UserComponent implements OnInit {
  constructor() {
    console.log('constructor is started');
  }
  ngOnInit() {
    console.log('onInit is applied');
  }
}
Задание 1

သီးသန့် method show ကိုဖန်တီးပြီး component စတင်အလုပ်လုပ်စဉ်တွင် ၎င်းကိုခေါ်ယူပါ။