4.0.0 • Published 4 years ago

@writetome51/unsubscribe-on-destroy-component v4.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

UnsubscribeOnDestroyComponent

An abstract Angular component class that any Angular component class which uses
Subscriptions may want to 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 UnsubscribeOnDestroyComponent 
    implements AfterViewInit {

    ngAfterViewInit() {
        this._subscriptions = this._subscriptions.concat(
            // Add any _subscriptions this component is using to this array:
            [
                this.subscriptionOne,
                this.subscriptionTwo,
                this.subscriptionThree
            ]
        );
    }

}

Properties

protected _subscriptions: Array<{ unsubscribe: () => any }>

Methods

ngOnDestroy() : void
    // Angular lifecycle hook.

Installation

npm i @writetome51/unsubscribe-on-destroy-component

Loading

import { UnsubscribeOnDestroyComponent } 
	from '@writetome51/unsubscribe-on-destroy-component';

License

MIT