浏览器刷新事件的监听和使用

浏览器F5刷新的时候有一个刷新执行之前的事件,beforeunload 事件,这个事件可以提示用户在刷新页面之前有一个提示。

下面是beforeunload的用法:

首先在methods中定义beforeunload事件

beforeunloadHandler(e) {
// e.preventDefault()
// e.returnValue = ‘’
e.returnValue = ‘确’
}

然后监听这个事件注意这个事件是在mouted()中调用

boforeunload() {
window.addEventListener(‘beforeunload’, this.beforeunloadHandler, false)
},

这个事件在deactivated()中销毁

destroy() {
window.removeEventListener(‘beforeunload’, this.beforeunloadHandler, false)
}

亲测beforeunload 事件只要触发不销毁,进入别的页面就会触发beforeunload 事件,如果不触发进入别的页面没有这个事件。

之前在项目中遇到这个问题,希望对大家有用。

如果您觉得文章有用,可以打赏个咖啡钱

扫描二维码关注公众号,回复: 6191479 查看本文章

在这里插入图片描述

每天成长一点点

猜你喜欢

转载自blog.csdn.net/weixin_43064669/article/details/89330452