![](/img/trans.png)
[英]How can I handle browser tab close event in Angular? Only close, not refresh
[英]Angular4: handle browser refresh/close event
在我的Angular4应用程序中,我想处理浏览器刷新事件。 刷新后,我想弹出确认警报,一旦按OK,就想做一些动作(调用一些api调用)。 我在下面尝试过。
// imports'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
@HostListener("window:beforeunload", ["$event"]) unloadHandler(event: Event) {
console.log("Processing beforeunload...");
event.returnValue = true;
}
constructor() {
}
}
现在,我可以在下面按键盘上的F5键。 但是我想显示确认警报并按“确定”按钮执行一些操作。
我想从用户中弹出自定义对话框,询问一些问题(例如:“这将释放您打开的每个预订的锁。您要继续吗?”),一旦用户单击“确定”按钮,就想调用一些api调用。
任何建议表示赞赏。
谢谢。
在这种情况下,显示“您所做的更改可能不会保存”对话框是Web浏览器唯一允许的操作,因此无法进行任何其他操作。
使用Internet Explorer,您可以将event.returnValue
设置为字符串值,并且该字符串将显示给用户,以鼓励他们不要重新加载/离开,但Chrome会忽略该信息并始终显示相同的消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.