3.0.1 • Published 6 years ago

ng2-rx-componentdestroyed v3.0.1

Weekly downloads
391
License
Apache-2.0
Repository
github
Last release
6 years ago

Build Status npm version

New version 3.0.0 breaking change

  • Requires >= RxJS 6.0.0 (part of Angular 6)

Unsubscribe from Observables in Angular Components

This small library provides a utility method that helps to unsubscribe from ReactiveX's Observables in Angular Components.

Demo

@Component({
  selector: 'foo',
  templateUrl: './foo.component.html'
})
export class FooComponent implements OnInit, OnDestroy {

  ngOnInit() {
    Observable.interval(1000)
        .pipe(
            untilComponentDestroyed(this) // <--- magic is here!
        )
        .subscribe(console.log);
  }

  ngOnDestroy() {
  }
  
}

Installation / Usage

Download the NPM package

npm i --save ng2-rx-componentdestroyed

Prepare the Angular Component class

The component class must have a ngOnDestroy() method (it can be empty):

@Component({
  selector: 'foo',
  templateUrl: './foo.component.html'
})
export class FooComponent implements OnDestroy {

  // ...

  ngOnDestroy() {
  }
  
}

Usage

Use the untilComponentDestroyed() method as an Observable pipe operator. This only works inside Angular components since this library uses the component's life cycle hooks to determine when the Observable is not needed anymore.

import {untilComponentDestroyed} from "ng2-rx-componentdestroyed";

...
...

Observable.interval(1000)
    .pipe(
        untilComponentDestroyed(this)
    )
    .subscribe(console.log);

More information

https://medium.com/thecodecampus-knowledge/the-easiest-way-to-unsubscribe-from-observables-in-angular-5abde80a5ae3

3.0.1

6 years ago

3.0.0

6 years ago

2.1.0

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago