3.0.1 • Published 5 years ago

ngx-auto-unsubscribe v3.0.1

Weekly downloads
5,496
License
MIT
Repository
github
Last release
5 years ago

Angular - Auto Unsubscribe For Pros

npm Build Status Build Status npm Awesome

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

OptionDescriptionDefault Value
arrayNameunsubscribe from subscriptions only in specified array''
blackListan array of properties to exclude[]
eventa name of event callback to execute onngOnDestroy

Note: blackList is ignored if arrayName is specified.

Similar projects

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