Higher-oder component to automatically unsubscribe from observables in Angular component classes.
Your Angular project must be running at least Angular version 9.
If that is not a problem, install the library:
npm i ngx-hocs-unsubscriber
autoComplete in your component class.
Unsubscriber as a Typescript Class decorator and
autoComplete(this) as a RxJS operator, right at the end of your pipe/s.
As simple as that.
You do not need to implement the
OnDestroy interface so you would only need to write the
ngOnDestroy() method if you needed to add extra actions to that Angular lifecycle hook.
autoCompletealways at the end of the pipe.
- Do not implement
OnDestroyif you do not need extra actions. The purpose of this library is no only to avoid memory leaks but also the unsubscription boilerplate.
- If you use
sharedReplaydo not forget to set
To avoid surprises it is recommended that you always use the signature that takes the config parameter in
If you want to make sure of that, you can use TSLint for that:
npm i -D rxjs-tslint-rules
And in your
You have resolved the unsubscription problem with 2 lines of code:
Happy Angular coding!