3 回答
TA贡献1772条经验 获得超8个赞
使用来自stewdebaker的@Hostlistener的示例确实运行良好,但是我对其进行了另一处更改,因为IE和Edge在MyComponent类上向最终用户显示了canDeactivate()方法返回的“ false”。
零件:
import {ComponentCanDeactivate} from "./pending-changes.guard";
import { Observable } from 'rxjs'; // add this line
export class MyComponent implements ComponentCanDeactivate {
canDeactivate(): Observable<boolean> | boolean {
// insert logic to check if there are pending changes here;
// returning true will navigate without confirmation
// returning false will show a confirm alert before navigating away
}
// @HostListener allows us to also guard against browser refresh, close, etc.
@HostListener('window:beforeunload', ['$event'])
unloadNotification($event: any) {
if (!this.canDeactivate()) {
$event.returnValue = "This message is displayed to the user in IE and Edge when they navigate without using Angular routing (type another URL/close the browser/etc)";
}
}
}
- 3 回答
- 0 关注
- 834 浏览
添加回答
举报