当前位置:网站首页>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
边栏推荐
- UXDB在不知道明文密码的情况下重置密码
- Detailed explanation of zero length array in C language (1) [information at the end of the article]
- px rem em
- Redis 事务学习有感
- Redis transaction learning
- A string "0" was actually the culprit of the collapse of station b
- Automated test series selenium three kinds of waiting for detailed explanation
- Moonpdflib Preview PDF usage record
- #648 (Div. 2)(A. Matrix Game、B. Trouble Sort、C. Rotation Matching)
- record
猜你喜欢

Amd epyc 9654 Genoa CPU cache test exposure L1 bandwidth up to 30tb/s

Pads copper laying
![Yolov7:oserror: [winerror 1455] the page file is too small to complete the final solution of the operation](/img/e1/51f750d355b248ab84e10f0e134271.png)
Yolov7:oserror: [winerror 1455] the page file is too small to complete the final solution of the operation

Unity slider slider development

Windows security hardening -- close unnecessary ports

Multi merchant mall system function disassembly Lecture 14 - platform side member level

7.14 - daily question - 408

js && ||

paddlepaddle论文系列之Alexnet详解(附源码)

Vscode installation and configuration
随机推荐
Unity slider slider development
7.20 - daily question - 408
Nacos hand to hand teaching [i] dynamic configuration of Nacos
The troubleshooting process of a segment error (disassembly address troubleshooting)
Notes on topic brushing (XXII) -- Dynamic Planning: basic ideas and topics
Invitation letter | "people, finance, tax" digital empowerment, vigorously promote retail enterprises to achieve "doubling" of economies of scale
Windows security hardening -- close unnecessary ports
How to use measurement data to drive the improvement of code review
Pytorch structure reparameterization repvggblock
进程的几种状态
BGP机房和BGP
Which automation tools can double the operation efficiency of e-commerce?
Find the median of two numbers in the fourth question of C language deduction (three methods)
How to better use the touchpad of notebook
"Usaco2006nov" corn fields solution
Multi merchant mall system function disassembly Lecture 14 - platform side member level
7.15 - daily question - 408
The IPO of Tuba rabbit was terminated: the annual profit fell by 33%, and Jingwei Sequoia was the shareholder
7.18 - daily question - 408
Batchinsert avoid inserting duplicate data ignor