我想检测页面何时在vue.js中刷新或重新加载。我已经阅读了这篇文章中的问题,请在vue.js中重新加载或关闭之前执行一些操作。它使用window.onbeforeunload。但是对我来说,问题是,该窗口函数放在哪里?我们可以使用vuejs中的另一个默认方法来检测刷新的页面吗?谢谢
You can use the created function to call another function. Example:
created() {
window.addEventListener('beforeunload', this.handler)
},
methods: {
handler: function handler(event) {
// Do Something
}
}
or
created() {
window.addEventListener('beforeunload', function(event) {
event.returnValue = 'Write something'
})
},
Run Code Online (Sandbox Code Playgroud)
Check it : https://forum.vuejs.org/t/detect-browser-close/5001/8