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:

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

Kết nối chúng với lớp:

app/user/user.component.tsexport class UserComponent implements OnInit, OnDestroy {
  
}

Hãy viết hai hook của chúng ta:

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

Kiểm tra hoạt động của cả hai hook trên điều kiện phản ứng if.

Задание 2

Kiểm tra hoạt động của cả hai hook trên vòng lặp phản ứng.