Skip to content

takeUntil

takeUntil completes an Observable when a notifier Observable emits. It’s the standard pattern for subscription cleanup.

@Component({ ... })
export class MyComponent implements OnDestroy {
private destroy$ = new Subject<void>();
ngOnInit() {
this.service.data$.pipe(
takeUntil(this.destroy$)
).subscribe(data => this.data = data);
}
ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
}

takeUntil is the recommended pattern for preventing memory leaks.”