Do something before reload or close in vue.js

后端 未结 1 1875
星月不相逢
星月不相逢 2021-01-11 17:24

I have a Vue.JS application with a beforeDestroy method that I\'m trying to use to store some data in local storage before the app closes or is reloaded.

1条回答
  •  一整个雨季
    2021-01-11 17:51

    What is it that "closes" or "reloads" the app? Is it the user in the browser closing the window and/or refreshing the page?

    If that is the case, nothing is "destroyed" and that function won't run.

    You would have to define a onbeforeunload function globally:

    window.onbeforeunload = function(){
        return "Are you sure you want to close the window?";
    }
    

    Then you can save items to local storage inside that function before the return statement.

    Note: don't define it inside your beforeDestroy function, because again, that just won't run when you reload the page or navigate away.

    0 讨论(0)
提交回复
热议问题