https://github.com/writetome51/unsubscribe-on-destroy-directive
Abstract Angular class that unsubscribes from all subscriptions during ngOnDestroy()
https://github.com/writetome51/unsubscribe-on-destroy-directive
abstract abstract-class angular class component typescript unsubscribe
Last synced: about 1 month ago
JSON representation
Abstract Angular class that unsubscribes from all subscriptions during ngOnDestroy()
- Host: GitHub
- URL: https://github.com/writetome51/unsubscribe-on-destroy-directive
- Owner: writetome51
- License: mit
- Created: 2019-01-02T06:47:45.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-06-02T21:06:55.000Z (about 4 years ago)
- Last Synced: 2025-10-24T16:56:42.096Z (8 months ago)
- Topics: abstract, abstract-class, angular, class, component, typescript, unsubscribe
- Language: TypeScript
- Homepage:
- Size: 21.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# UnsubscribeOnDestroyDirective
An abstract Angular directive class that any Angular directive class which uses
[Subscriptions](https://rxjs-dev.firebaseapp.com/api/index/class/Subscription) can extend
from.
During the `ngOnDestroy()` hook, it unsubscribes from all Subscriptions inside
`this._subscriptions`.
Have your component classes that use Subscriptions
inherit from this class, add their subscriptions to `this._subscriptions`, then let
this class automatically unsubscribe from all of them during `ngOnDestroy()`.
## Example
```
export class ExamplePageComponent extends UnsubscribeOnDestroyDirective
implements AfterViewInit {
ngAfterViewInit() {
this._subscriptions = this._subscriptions.concat(
// Add any subscriptions this component is using to this array:
[
this.subscriptionOne,
this.subscriptionTwo,
// ...
]
);
}
}
```
## Properties
```
protected _subscriptions: Array<{ unsubscribe: () => any }>
```
## Methods
```
ngOnDestroy() : void
```
## Installation
`npm i @writetome51/unsubscribe-on-destroy-directive`
## Loading
```ts
import { UnsubscribeOnDestroyDirective }
from '@writetome51/unsubscribe-on-destroy-directive';
```
## License
[MIT](https://choosealicense.com/licenses/mit/)