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
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With