Skip to content

Instantly share code, notes, and snippets.

@blove
Created December 29, 2020 00:28
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 blove/52eded129083a43b07b32c70884b80e5 to your computer and use it in GitHub Desktop.
Save blove/52eded129083a43b07b32c70884b80e5 to your computer and use it in GitHub Desktop.
Angular BaseComponent
import { Directive, OnDestroy } from '@angular/core';
import { Subject } from 'rxjs';
@Directive()
// tslint:disable-next-line:directive-class-suffix
export abstract class BaseComponent implements OnDestroy {
private destroy$: Subject<void> | null = null;
get destroy() {
if (!this.destroy$) {
this.destroy$ = new Subject();
}
return this.destroy$.asObservable();
}
ngOnDestroy() {
if (this.destroy$) {
this.destroy$.next();
this.destroy$.complete();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment