当前位置:网站首页>How to empty localstorage before closing a page
How to empty localstorage before closing a page
2022-07-25 01:01:00 【Front end junior brother】
article の Catalog
One 、 Preface
In practice Vue In the project, we want to do persistence , Will use vuex coordination localstorage、cookie、 And so on , There will be no data loss on the refresh page ;localstorage The feature is that the life cycle is permanent unless you manually clear , This can lead to a problem , For example, you want to clear the local storage content when closing the page , We need to deal with it , The following will combine vue The problems encountered in the project provide solutions ;
Two 、 Problem description
stay vue In order to do access permission in the project , You need to inject token, On the contrary, when you log out , The locally stored token, But if you use locastorage When storing data, a bug, That is, when you do not exit the page through the exit button , Instead, clicking the close button directly to close the page will cause a problem : Locally stored token The value still exists ; At this point, if you do route authentication in the route front guard, it will lead to the next page , The front guard of the route found that there were still token It will jump directly to the welcome interface . For this problem, let me provide you with a solution ;
3、 ... and 、 terms of settlement
The key to solving the above problem is obviously how to listen for the event of closing the page , Just close the page and clear the locally stored token;
All we need to do is App Component's mounted Inside the hook window Bind one beforeunload event , Clear in the callback function token, As shown in the figure below ;
The code example is as follows :
mounted () {
// Empty the browser cache when closing the browser window localStorage The data of
window.onbeforeunload = function (e) {
const storage = window.localStorage
storage.clear()
}
}
At the end
(*´◡`*) Your give the thumbs-up Comment on Collection Is the greatest driving force of the author's creation
边栏推荐
- MySQL的最左前缀原则
- Password input box and coupon and custom soft keyboard
- Method properties of ASP adodb.stream object
- Brush questions of binary tree (5)
- Cloud native observability tracking technology in the eyes of Baidu engineers
- The current situation of the industry is disappointing. After working, I returned to UC Berkeley to study for a doctoral degree
- The model needs to use two losses_ FN, how to operate?
- Sort out some scattered knowledge points by yourself
- Several states of the process
- ROS manipulator movelt learning notes 3 | kinect360 camera (V1) related configuration
猜你喜欢

基于ABP实现DDD--领域逻辑和应用逻辑

Latest information of 2022 cloud computing skills competition

What is the function of transdata operator and whether it can optimize performance

Uncaught typeerror: cannot read properties of null (reading 'append') solution

What does it operation and maintenance management mean? How to establish an effective IT operation and maintenance management system?

Document the use of anti shake in packaged components and projects

Listing of China graphite: the market value is nearly HK $1.2 billion, achieving a zero breakthrough in the listing of Hegang private enterprises

Esp32 OLED lvgl displays common Chinese characters

Harbor installation

Vegetable greenhouses turned into smart factories! Baidu AI Cloud helps Shouguang, Shandong build a new benchmark for smart agriculture
随机推荐
UXDB在不知道明文密码的情况下重置密码
Join MotoGP Monster Energy British Grand Prix!
paddlepaddle论文系列之Alexnet详解(附源码)
ROS manipulator movelt learning notes 3 | kinect360 camera (V1) related configuration
Chip sold at sand price: Lei Jun's dream was "ruined" by this company
WhatsApp web for usability testing of software testing technology
Number of palindromes in question 5 of C language deduction (two methods)
Which automation tools can double the operation efficiency of e-commerce?
Batchinsert avoid inserting duplicate data ignor
Notes on topic brushing (XXII) -- Dynamic Planning: basic ideas and topics
The first meta universe auction of Chen Danqing's printmaking works will open tomorrow!
Vscode installation and configuration
如何创建索引
Nodejs package
第四章 驱动子系统开发
Director of Shanghai Bureau of culture and Tourism: safety is the lifeline of culture and tourism, and we are seizing the new track of yuancosmos
C recursively obtains all files under the folder and binds them to the treeview control
Redis管道技术/分区
asp rs.open sql,conn,3,1中3,1代表什么?
Detailed explanation of zero length array in C language (1) [information at the end of the article]