💡 The most important ones are:
All events:
ngOnChanges()
: incoming input; SimpleChanges
does NOT support generic <T>
(feature request since 2017)ngOnInit()
: component init, fetch data here if you mustngDoCheck()
: it runs on every detection, so avoid it like the plague, unless you really know what you're doing ⛔ngAfterContentInit()
: transcluded content initializedngAfterContentChecked()
ngAfterViewInit()
: the view (along with children) has fully been initialized ngAfterViewChecked()
ngOnDestroy()
: clean up your subscribers and event listeners here@Component({ selector: 'app-sample', template: `...` })
class Sample implements OnInit, OnChanges, OnDestroy {
ngOnInit(): void {}
ngOnChanges(changes: SimpleChanges): void {}
ngOnDestroy(): void {}
}
| async
pipedestroyed = new Subject()
helper (then used takeUntil
and this.destroyed.next()
)@UntilDestroy
is a popular way of doing unsubscribes (using decorators)@UntilDestroy()
.pipe(untilDestroyed(this)).subscribe(...)
takeUntilDestroy
.pipe(takeUntilDestroyed()).subscribe(...)
DestroyRef
under the hood, which is needed if you're not inside an injection context:destroyRef = inject(DestroyRef)
.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(...)