# ng2-rx-componentdestroyed

> [![Build Status](https://travis-ci.org/w11k/ng2-rx-componentdestroyed.svg?branch=master)](https://travis-ci.org/w11k/ng2-rx-componentdestroyed) [![npm version](https://badge.fury.io/js/ng2-rx-componentdestroyed.svg)](https://badge.fury.io/js/ng2-rx-compon

Latest version **3.0.1** (published 2018-09-03) · Apache-2.0 license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install ng2-rx-componentdestroyed
pnpm add ng2-rx-componentdestroyed
yarn add ng2-rx-componentdestroyed
bun add ng2-rx-componentdestroyed
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 3.0.1 |
| Published | 2018-09-03 |
| First published | 2017-05-24 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 21.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 95 |
| Maintainers | romanroe |

## Links

- npm: https://www.npmjs.com/package/ng2-rx-componentdestroyed
- Repository: https://github.com/w11k/ng2-rx-componentdestroyed
- Homepage: https://github.com/w11k/ng2-rx-componentdestroyed#readme
- Issues: https://github.com/w11k/ng2-rx-componentdestroyed/issues
- npm.io page: https://npm.io/package/ng2-rx-componentdestroyed

## Dependencies (1)

- [rxjs](https://npm.io/package/rxjs.md) ^6.0.0

## Recent versions

- 3.0.1 (latest) — 2018-09-03
- 3.0.0 — 2018-05-16
- 2.1.0 — 2018-03-16
- 2.0.1 — 2018-02-13
- 2.0.0 — 2018-02-13
- 1.1.2 — 2017-08-29
- 1.1.1 — 2017-06-13
- 1.1.0 — 2017-06-07
- 1.0.11 — 2017-06-02
- 1.0.10 — 2017-06-02
- 1.0.9 — 2017-06-02
- 1.0.8 — 2017-06-02
- 1.0.7 — 2017-06-02
- 1.0.6 — 2017-06-02
- 1.0.5 — 2017-06-02
- … 3 more at https://npm.io/package/ng2-rx-componentdestroyed/versions

## README

[![Build Status](https://travis-ci.org/w11k/ng2-rx-componentdestroyed.svg?branch=master)](https://travis-ci.org/w11k/ng2-rx-componentdestroyed)
[![npm version](https://badge.fury.io/js/ng2-rx-componentdestroyed.svg)](https://badge.fury.io/js/ng2-rx-componentdestroyed)


# 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

---
_Source: https://npm.io/package/ng2-rx-componentdestroyed · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
