在vue.js中刷新页面的方法

Hyo*_*ipo 2 vue.js

我想检测页面何时在vue.js中刷新或重新加载。我已经阅读了这篇文章中的问题,请在vue.js中重新加载或关闭之前执行一些操作。它使用window.onbeforeunload。但是对我来说,问题是,该窗口函数放在哪里?我们可以使用vuejs中的另一个默认方法来检测刷新的页面吗?谢谢

rub*_*ero 5

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

  • 不要忘记在 beforeDestroy 上删除添加的事件侦听器。否则,您可能会有大量未使用的事件侦听器,从而降低您的应用程序的速度。 (2认同)