我正在尝试使用window.onbeforeunload
在我的项目中,但我不知道如何使用它。
我尝试过两种方式:
1st way
$(window).on("beforeunload", function(e) {
return "Are you sure you want leave?";
});
2nd way
window.onbeforeunload = function (e) {
// e.returnValue = "hello, are you sure???";
return "You may lose the changes!!!\n are you sure???";
// return null;
};
我在它们两个中都遇到错误,我猜这是因为我没有在某个地方导入某些东西。
此外,我想知道调用此事件时是否可以重定向到我网站的特定 URL。
示例:按 F5 --> /dashboard/
你必须导入@HostListener
并在您的组件中像下面一样使用它
import { Component, OnInit, HostListener } from '@angular/core';
export class TestComponent implements OnInit {
@HostListener('window:beforeunload', ['$event']) onBeforeUnload(event) {
// do something here
console.log("method called");
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)