Fix browser unsaved changes with custom guard

This commit is contained in:
Michael Shamoon
2022-08-06 19:30:39 -07:00
parent cdc7a5842e
commit ea52490f6b
7 changed files with 77 additions and 32 deletions

View File

@@ -0,0 +1,20 @@
import { CanDeactivate } from '@angular/router'
import { Injectable } from '@angular/core'
import { Observable } from 'rxjs'
export interface ComponentCanDeactivate {
canDeactivate: () => boolean | Observable<boolean>
}
@Injectable()
export class DirtyDocGuard implements CanDeactivate<ComponentCanDeactivate> {
canDeactivate(
component: ComponentCanDeactivate
): boolean | Observable<boolean> {
return component.canDeactivate()
? true
: confirm(
$localize`Warning: You have unsaved changes to your document(s).`
)
}
}