当前位置:网站首页>在beforeDestroy中销毁localStorage中的值无效
在beforeDestroy中销毁localStorage中的值无效
2022-07-02 21:32:00 【bug丸】
beforeDestroy中销毁localStorage中的数据无效
目的:在关闭窗口时删除localStorage中的数据
方法1:beforeDestroy中删除
beforeDestroy() {
localStorage.removeItem("info");
}
想要在组件销毁的时候删除localStorage中的变量,结果发现无效
beforeDestroy在页面刷新的时候不会执行,但是在页面跳转的时候会执行(this. r o u t e r . p u s h , t h i s . router.push,this. router.push,this.router.replace),但是关闭窗口的时候不会执行
该强制关闭窗口的时候,浏览器都会在js执行的间隙立马中止所有后续js执行(vue的beforeDestroy也不例外),并关闭该窗口。
方法2:unload事件中删除
mounted() {
window.addEventListener("unload", () => {
localStorage.removeItem("hello");
});
},
可以达到删除的效果,但是有一个问题,就是页面刷新的时候也会执行,这个不是想要的结果
最后还是改为放到sessionStorage中了,还是回到了最初的想法
onload onunload beforeunload的执行时机
页面加载时只执行onload
页面关闭时先执行onbeforeunload,最后onunload
页面刷新时先执行onbeforeunload,然后onunload,最后onload
边栏推荐
- 【剑指 Offer 】56 - II. 数组中数字出现的次数 II
- Research Report on micro gripper industry - market status analysis and development prospect prediction
- Off chip ADC commissioning record
- [hands on deep learning]02 softmax regression
- Today, I met a Alipay and took out 35K. It's really sandpaper to wipe my ass. it's a show for me
- The web version of xshell supports FTP connection and SFTP connection [detailed tutorial] continued from the previous article
- China plastic bottle and container market trend report, technological innovation and market forecast
- B.Odd Swap Sort(Codeforces Round #771 (Div. 2))
- Construction and maintenance of business websites [10]
- [CV] Wu Enda machine learning course notes | Chapter 12
猜你喜欢
![[use of pointer and pointer and array]](/img/dd/8017215c54aebcdf5c67e46e795d3b.jpg)
[use of pointer and pointer and array]

Basic IO interface technology - microcomputer Chapter 7 Notes

MySQL learning record (7)
![[CV] Wu Enda machine learning course notes | Chapter 12](/img/c8/9127683b6c101db963edf752ffda86.jpg)
[CV] Wu Enda machine learning course notes | Chapter 12

发现你看不到的物体!南开&武大&ETH提出用于伪装目标检测SINet,代码已开源!...

Off chip ADC commissioning record

D4:非成对图像去雾,基于密度与深度分解的自增强方法(CVPR 2022)

Gbase8s database type

One week dynamics of dragon lizard community | 2.07-2.13

Hot backup routing protocol (HSRP)
随机推荐
[shutter] shutter gesture interaction (click event handling | click OnTap | double click | long press | click Cancel | press ontapdown | lift ontapup)
[shutter] statefulwidget component (image component | textfield component)
Research Report on market supply and demand and strategy of China's Plastic Geogrid industry
The web version of xshell supports FTP connection and SFTP connection
Plastic floating dock Industry Research Report - market status analysis and development prospect forecast
In depth research and investment feasibility report on the global and China active vibration isolation market 2022-2028
Construction and maintenance of business website [1]
AES encryption CBC mode pkcs7padding filling Base64 encoding key 32byte iv16byte
Construction and maintenance of business website [2]
Detailed explanation of OSI seven layer model
How to prevent your jar from being decompiled?
Analysis of enterprise financial statements [4]
Adding data to the head or tail of the rar file can still decompress normally
[shutter] shutter layout component (wrap component | expanded component)
MySQL learning record (8)
[shutter] shutter layout component (physicalmodel component)
[shutter] statefulwidget component (bottom navigation bar component | bottomnavigationbar component | bottomnavigationbaritem component | tab switching)
Accounting regulations and professional ethics [18]
treevalue——Master Nested Data Like Tensor
Market trend report, technical innovation and market forecast of China's Micro pliers