angular7 - 在Angular 7中处理浏览器主页按钮单击事件
问题描述
我正在使用Angular 7
并努力获取浏览器主页按钮单击事件。
场景:- 当用户单击浏览器的主页按钮时,我需要向用户显示确认消息,询问用户是否要保存更改。如果"Yes"
那么用户将停留在当前页面上,如果点击"No"
则重定向到浏览器默认页面,或者可能会被路由到其他页面。
当我在谷歌上搜索时,我得到的大部分链接都是浏览器后退按钮,这在这种情况下没有帮助。
到目前为止,我已经尝试了以下无法正常工作的解决方案,因为它无法捕获并保留刷新和主页按钮的处理程序。
1.
@HostListener("window:beforeunload", ["$event"]) unloadHandler(event: Event)
{
event.returnValue = false;
}
2.
//check for Navigation Timing API support
if (window.performance) {
console.info("window.performance works fine on this browser");
}
if (performance.navigation.type == 1) {
confirm('This is performance Api');
console.info( "This page is reloaded" );
} else {
console.info( "This page is not reloaded");
}
3.
this.router.events.subscribe((evt) => {
if (evt instanceof NavigationEnd) {
//trick the Router into believing it's last link wasn't previously loaded
this.router.navigated = false;
if (!this.router.navigated){
console.log(browserRefresh);
}
//if you need to scroll back to top, here is the right place
window.scrollTo(0, 0);
}
});
请帮忙。
解决方案
恐怕您所寻求的行为是不可能的。如果可以覆盖beforeunload
默认行为(或类似行为),那么恶意网站可能会诱使用户认为他们已经离开了页面,而实际上他们并没有。
实际上,过去可以自定义显示的消息,但此功能已被删除(在 2016 年)以防止此类骗局:
推荐阅读
- javascript - 是否可以在 After Effects 中使用 URL 读取图像?
- bash - grep:文件中的行没有这样的文件或目录
- android - FireBase 按文档和集合进行多个查询
- node.js - 如何离开带有 vue-socket 的套接字房间并重新加入而不重复消息?
- c++ - 使用自定义类型定义多个同名函数
- azure - ADF 活动未返回正确的错误代码
- c++ - 如何在地图中使用位集?
- julia - 以有效的方式从字符串值转换为浮点值与缺失值的 DataFrame 列
- powershell - 如何在 PowerShell 中获取 Windows.System.Profile.SystemIdentification.Id?
- mongodb - 查询以从 mongodb 对象的嵌套数组中获取 id