Created
May 27, 2021 18:32
-
-
Save TaylorAckley/6459e33e35f5db6ca3707c2273bf359a to your computer and use it in GitHub Desktop.
This file contains 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 { HttpClient } from '@angular/common/http'; | |
import { Component, Input, OnDestroy, OnInit } from '@angular/core'; | |
import { Subject } from 'rxjs'; | |
import { takeUntil } from 'rxjs/operators'; | |
@Component({ | |
selector: 'hello', | |
template: `<h1>Hello!</h1>`, | |
styles: [`h1 { font-family: Lato; }`] | |
}) | |
export class HelloComponent implements OnInit, OnDestroy { | |
$unsubscribe = new Subject<void>(); | |
constructor(private http: HttpClient) {} | |
ngOnInit() { | |
this.getData() | |
} | |
getData() { | |
this.http.get('some-endpoint') | |
.pipe(takeUntil(this.$unsubscribe)) | |
.subscribe(response => console.log(response)) | |
} | |
ngOnDestroy() { | |
this.$unsubscribe.next(); | |
this.$unsubscribe.complete() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment