【发布时间】:2019-06-09 12:38:33
【问题描述】:
我需要制作一个简单的确认窗口,并且我看到了很多关于如何通过额外操作来做到这一点的示例(比如等到表单的文件上传不是字段)。但是我只需要创建一个带有默认文本的默认确认窗口(如下图所示),以便在用户想要从当前页面离开时显示它。而且我无法完全理解在处理before unload 事件时应该证明什么逻辑。
如果这是一些问题的重复,我最近很抱歉,但是,我没有找到任何解决方案。所以我有:
example.guard.ts
export interface CanComponentDeactivate {
canDeactivate: () => Observable<boolean> | boolean;
}
@Injectable()
export class ExampleGuard implements CanDeactivate<CanComponentDeactivate> {
constructor() { }
canDeactivate(component: CanComponentDeactivate): boolean | Observable<boolean> {
return component.canDeactivate() ?
true :
confirm('message'); // <<< does confirm window should appear from here?
}
}
example.component.ts
export class ExampleComponent implements CanComponentDeactivate {
counstructor() { }
@HostListener('window:beforeunload', ['$event'])
canDeactivate($event: any): Observable<boolean> | boolean {
if (!this.canDeactivate($event)) {
// what should I do here?
}
}
}
如果你能提供一个代码示例,那就太好了,但我很感激任何帮助。
【问题讨论】:
标签: angular typescript onbeforeunload angular-router-guards