หลายฮุกใน Angular
สามารถกำหนดหลายฮุกพร้อมกันได้ ลองมาดูวิธีการทำผ่านตัวอย่างของสองฮุกที่เราได้ศึกษามาแล้ว
นำเข้า Interface สองตัว:
import { OnInit, OnDestroy } from '@angular/core';
เชื่อมต่อพวกมันกับคลาส:
export class UserComponent implements OnInit, OnDestroy {
}
มาเขียนสองฮุกของเรา:
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
ทดสอบการทำงานของทั้งสองฮุกบนลูปปฏิกิริยา