Angularでの複数のフック
複数のフックを同時に設定することができます。 すでに学んだ2つのフックを例にして、その方法を見てみましょう。
2つのインターフェースをインポートします:
import { OnInit, OnDestroy } from '@angular/core';
これらをクラスに接続しましょう:
export class UserComponent implements OnInit, OnDestroy {
}
2つのフックを記述します:
export class UserComponent implements OnInit, OnDestroy {
constructor() {
console.log('constructor is started');
}
ngOnInit() {
console.log('onInit is applied');
}
ngOnDestroy() {
console.log('onDestroy is applied');
}
}
リアクティブな if 条件で両方のフックの動作を確認してください。
リアクティブなループで両方のフックの動作を確認してください。