import { CanDeactivate } from '@angular/router' ; import { from , Observable } from 'rxjs' ; import { Injectable } from '@angular/core' ; export interface CanComponentDeactivate { canDeactivate : ( ) => boolean | Promise < boolean > ; } @ Injectable ( { providedIn : 'root' } ) export class CanDeactivateGuard implements CanDeactivate < CanComponentDeactivate > { canDeactivate ( component ) { return component . canDeactivate ? this . toObservable ( component . canDeactivate ( ) ) : true ; } private toObservable ( deactivate ) { const p = Promise . resolve ( deactivate ) ; const o$ = from ( p ) ; return o$ ; } }