In the vue project, how to clear the data in localstorage when you close the browser, regardless of session

can you tell when you open the browser

Mar.02,2021

sessionStorage

is recommended < hr >

if you want to clear localStorage when you close the browser, there are several situations in which cleaning is not guaranteed:
1, using window. Onbeforeunload , although there is such an event in the browser, it is not always triggered. You can try to pop up a confirmation box in this event to try
2. There are many ways for users to close vue , for example, close the tag , close the browser , shut down the computer , suddenly shut down the computer , and so on. In many cases, there is no guarantee that the data in localStorage can be cleared when vue exits.

< hr >

if you must use localStorage , you might as well solve the problem differently. When the vue project starts, get the beforeCreate or created data stored locally in the outermost vue (usually App.vue) cycle. If exists (pay attention to if) , clear , and at the same time Then it will erase the data and provide one more guarantee.

< hr >
I hope my answer will be helpful to you!

// 
window.onbeforeunload = function (e) {
    // 
};

since you close your browser to clear, why not choose sessionStorage?


to refresh, please use the vuex or eventBus global variable.
Please use sessionStorage to empty the closed window without refreshing.
use localStorage only when you close the browser without emptying it.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e618e7-45953.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e618e7-45953.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?