Wiele hooków w Angularze

Można jednocześnie zdefiniować wiele hooków. Spójrzmy, jak to się robi na przykładzie dwóch już poznanych hooków.

Zaimportujmy dwa interfejsy:

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

Podłączmy je do klasy:

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

Napiszmy nasze dwa hooki:

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

Sprawdź działanie obu hooków na reaktywnym warunku if.

Задание 2

Sprawdź działanie obu hooków na reaktywnej pętli.