浏览器刷新事件的监听和使用 -----vue
浏览器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 事件,如果不触发进入别的页面没有这个事件。
本文来自博客园,作者:玖捌,转载请注明原文链接:https://www.cnblogs.com/fyh0912/p/14356965.html
浙公网安备 33010602011771号