Q:

How can I store data before one page closed in Vue

so far as I know, if you close or refresh a page,the hook 'beforedestroy' and 'destroyed' in Vue would not be triggered.So maybe I can use

window.onbeforeunload = function() { ... }

And here's the code
mounted() {
            window.onbeforeunload = function() {
                window.localStorage.setItem('form', JSON.stringify(this.form))
            }
        }

However, What I store is undefined. So I think that the instance has been destroyed when the function is called. In this way, this.form can't be accessed.Is there any other way?

A:

You are shadowing this keyword by defining an anonymous function, use an arrow function instead:
window.onbeforeunload = () => {
  window.localStorage.setItem('form', JSON.stringify(this.form))
}

