Skip to content
This repository has been archived by the owner on Jul 18, 2021. It is now read-only.

NetanelBasal/ngx-auto-unsubscribe

Repository files navigation

Angular - Auto Unsubscribe For Pros

npm Build Status Build Status npm Awesome

For Angular 9+, use until-destroy

Class decorator that will automatically unsubscribe from observable subscriptions when the component is destroyed

Installation

npm install ngx-auto-unsubscribe --save

Usage

import { AutoUnsubscribe } from "ngx-auto-unsubscribe";

@AutoUnsubscribe()
@Component({
  selector: 'inbox'
})
export class InboxComponent {
  one: Subscription;
  two: Subscription;

  constructor( private store: Store<any>, private element : ElementRef ) {}

  ngOnInit() {
    this.one = store.select("data").subscribe(data => // do something);
    this.two = Observable.interval.subscribe(data => // do something);
  }

  // This method must be present, even if empty.
  ngOnDestroy() {
    // We'll throw an error if it doesn't
  }
}

Options

Option Description Default Value
arrayName unsubscribe from subscriptions only in specified array ''
blackList an array of properties to exclude []
event a name of event callback to execute on ngOnDestroy

Note: blackList is ignored if arrayName is specified.

Similar projects

You can also use https://github.com/NetanelBasal/ngx-take-until-destroy.