Created
December 20, 2017 16:54
-
-
Save alxhub/b2b12c1c8fc258f993a828764555120d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Component, OnDestroy} from '@angular/core'; | |
import {HttpClient} from '@angular/common/http'; | |
import {Subject} from 'rxjs/Subject'; | |
import {takeUntil} from 'rxjs/operators/takeUntil'; | |
@Component({ | |
selector: '...', template: '...' | |
}) | |
export class MyCmp implements OnDestroy { | |
onDestroy$ = new Subject<void>(); | |
data: any; | |
constructor(http: HttpClient) { | |
this | |
.http | |
.get('/url') | |
.pipe( | |
takeUntil(this.onDestroy$) | |
) | |
.subscribe(res => this.data = res); | |
} | |
ngOnDestroy(): void { | |
this.onDestroy$.next(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment