Skip to content

Instantly share code, notes, and snippets.

@romanoffs
Created June 20, 2019 14:54
Show Gist options
  • Save romanoffs/302c01274fdfb1a53ce8f429de905796 to your computer and use it in GitHub Desktop.
Save romanoffs/302c01274fdfb1a53ce8f429de905796 to your computer and use it in GitHub Desktop.
Angular 7 ComponentCanDeactivate
import {HostListener} from '@angular/core';
import {Observable} from 'rxjs';
export abstract class ComponentCanDeactivate {
abstract get saved(): boolean;
abstract canDeactivate(): Observable<boolean> | Promise<boolean> | boolean
@HostListener('window:beforeunload', ['$event'])
unloadNotification($event: any): void {
if (!this.saved) {
$event.returnValue = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment