浏览器刷新事件的监听和使用
浏览器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 事件,如果不触发进入别的页面没有这个事件。
本文内容仅供个人学习/研究/参考使用,不构成任何决策建议或专业指导。分享/转载时请标明原文来源,同时请勿将内容用于商业售卖、虚假宣传等非学习用途哦~感谢您的理解与支持!