rainstormza / ngx-auto-unsubscribe

Class decorator that will automatically unsubscribe from observables

Home Page:https://netbasal.com/automagically-unsubscribe-in-angular-4487e9853a88

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Angular - Auto Unsubscribe For Pros

Build Status Build Status Awesome

Class decorator that will automatically unsubscribe from observables when the component 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);
  }
  
  // If you work with AOT this method must be present, even if empty! 
  // Otherwise 'ng build --prod' will optimize away any calls to ngOnDestroy, 
  // even if the method is added by the @AutoUnsubscribe decorator
  ngOnDestroy() {
    // You can also do whatever you need here
  }
}

About

Class decorator that will automatically unsubscribe from observables

https://netbasal.com/automagically-unsubscribe-in-angular-4487e9853a88


Languages

Language:TypeScript 100.0%