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

浏览器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 事件,如果不触发进入别的页面没有这个事件。

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

 

 个人vuepress搭建博客 https://wojiaozhangyonghao.github.io/myblog/

加群教你用vue + vuepress 搭建自己的博客

加入技术交流群

扫描二维码

posted @ 2019-04-15 10:45  别闹啊  阅读(14159)  评论(2编辑  收藏  举报
Live2D