import { Injectable } from '@angular/core'; import { ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, CanDeactivate } from '@angular/router'; import { Observable } from 'rxjs'; export interface GuardedComponent { destroyMsg: string; canDestroy(): boolean; } @Injectable({ providedIn: 'root', }) export class ConfirmDeactivateGuardGuard implements CanDeactivate { canDeactivate(component: GuardedComponent, currentRoute: ActivatedRouteSnapshot, currentState: RouterStateSnapshot, nextState?: RouterStateSnapshot): boolean | UrlTree | Observable | Promise { // console.log('deactivate check'); if (component.canDestroy()) { return true; } else { return window.confirm(component.destroyMsg); } } }