Angular တွင် ngOnInit ဟုခေါ်သော Hook
Hook ngOnInit သည် component စတင်အလုပ်လုပ်သည့်အခါတွင်အလုပ်လုပ်သည်။
၎င်း၏လုပ်ဆောင်ချက်မှာ class constructor နှင့်ဆင်တူသော်လည်း ပို၍ရှုပ်ထွေးသော
လုပ်ငန်းများကို ဥပမာ server မှဒေတာများကိုဆွဲချခြင်းကဲ့သို့သော လုပ်ငန်းများကို
ဤတွင်လုပ်ဆောင်နိုင်သည်။
ဤ hook ကိုမည်သို့အသုံးပြုရမည်ကိုကြည့်ရှုကြပါစို့။ ပထမဦးစွာ ၎င်း၏ interface ကို import လုပ်ရန်လိုအပ်ပါသည်။
import { OnInit } from '@angular/core';
ထို့နောက် interface ကို component class ထဲသို့ထည့်သွင်းရန်လိုအပ်ပါသည်။
export class UserComponent implements OnInit {
}
ထို့နောက် ကျွန်ုပ်တို့သည် component class ထဲတွင် ngOnInit method ကိုရေးသားနိုင်ပြီး
class စတင်အလုပ်လုပ်စဉ်တွင် ဤ method သည်အလိုအလျောက်လုပ်ဆောင်သွားမည်ဖြစ်သည်။
၎င်း၏လုပ်ဆောင်ချက်ကိုစစ်ဆေးကြည့်ရအောင်။
export class UserComponent implements OnInit {
constructor() {
console.log('constructor is started');
}
ngOnInit() {
console.log('onInit is applied');
}
}
သီးသန့် method show ကိုဖန်တီးပြီး component စတင်အလုပ်လုပ်စဉ်တွင် ၎င်းကိုခေါ်ယူပါ။