Skip to content

Instantly share code, notes, and snippets.

@TaylorAckley
Created May 27, 2021 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TaylorAckley/6459e33e35f5db6ca3707c2273bf359a to your computer and use it in GitHub Desktop.
Save TaylorAckley/6459e33e35f5db6ca3707c2273bf359a to your computer and use it in GitHub Desktop.
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