When should I store the “Subscription” instances and invoke unsubscribe() during the NgOnDestroy life cycle and when can I simply ignore them?

Technology CommunityCategory: AngularWhen should I store the “Subscription” instances and invoke unsubscribe() during the NgOnDestroy life cycle and when can I simply ignore them?
VietMX Staff asked 3 years ago

Se should add a

private ngUnsubscribe: Subject = new Subject();

field to all components that have .subscribe() calls to Observables within their class code.

We then call

this.ngUnsubscribe.next(); 
this.ngUnsubscribe.complete();

in our ngOnDestroy() methods.