Nhiều Hook trong Angular
Có thể đồng thời thiết lập nhiều hook. Hãy xem cách thực hiện điều này thông qua ví dụ về hai hook đã học.
Import hai interface:
import { OnInit, OnDestroy } from '@angular/core';
Kết nối chúng với lớp:
export class UserComponent implements OnInit, OnDestroy {
}
Hãy viết hai hook của chúng ta:
export class UserComponent implements OnInit, OnDestroy {
constructor() {
console.log('constructor is started');
}
ngOnInit() {
console.log('onInit is applied');
}
ngOnDestroy() {
console.log('onDestroy is applied');
}
}
Kiểm tra hoạt động của cả hai hook trên điều kiện
phản ứng if.
Kiểm tra hoạt động của cả hai hook trên vòng lặp phản ứng.