Performance: Component lifecycle overview
This concept affects how quickly components appear and respond during page load and user interaction.
ngOnInit() { scheduleAsyncTask(); useAngularBindingsForDOMUpdates(); }ngOnInit() { heavySyncTask(); updateDOMDirectly(); }| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy synchronous work in ngOnInit | Multiple direct DOM manipulations | Triggers multiple reflows | High paint cost due to layout thrashing | [X] Bad |
| Asynchronous tasks with Angular bindings | Minimal DOM operations via bindings | Single reflow after async completion | Low paint cost | [OK] Good |
ngOnInit() runs once after Angular sets component input properties.ngOnDestroy() runs before component removal, ngAfterViewInit() after view initialization, ngDoCheck() on every change detection.OnDestroy lifecycle hook in an Angular component?ngOnDestroy().OnDestroy requires defining ngOnDestroy() exactly.export class SampleComponent implements OnInit, OnDestroy {
ngOnInit() {
console.log('Init');
}
ngOnDestroy() {
console.log('Destroy');
}
}ngOnInit() runs once when component initializes, logging 'Init'.ngOnDestroy() runs once before component is removed, logging 'Destroy'.export class TestComponent implements OnInit {
ngOnInit() {
console.log('Component initialized');
}
ngOnDestroy() {
console.log('Component destroyed');
}
}OnInit but defines ngOnDestroy() without implementing OnDestroy.ngOnDestroy(), the class should implement OnDestroy interface for clarity and correctness.ngAfterViewInit() runs after Angular initializes the component's view and child views.ngOnInit() runs earlier, ngAfterContentInit() runs after projected content, and ngDoCheck() runs repeatedly.Pass the test and this pattern moves unknown