Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect page is refreshed in vue.js

Tags:

vue.js

I would like to detect when page is refreshed or reloaded in vue.js. I have read the question from this post Do something before reload or close in vue.js. It's using window.onbeforeunload. But the problem for me, where do I put this window function ? Could we use another default method from vuejs for detecting the page that is refreshed ? Thank you

like image 239
Maryadi Poipo Avatar asked May 17 '18 01:05

Maryadi Poipo


People also ask

Can you force Vue JS to reload Rerender?

The best way to force Vue to re-render a component is to set a :key on the component. When you need the component to be re-rendered, you just change the value of the key and Vue will re-render the component.

How do you update data on a page without refreshing on the Vue JS?

What you need is vm-forceUpdate. Force the Vue instance to re-render. Note it does not affect all child components, only the instance itself and child components with inserted slot content.


1 Answers

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'
      })
    },

Check it : https://forum.vuejs.org/t/detect-browser-close/5001/8

like image 65
rubotero Avatar answered Sep 20 '22 14:09

rubotero