当前位置:网站首页>在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
边栏推荐
- Construction and maintenance of business websites [6]
- Golang embeds variables in strings
- Research Report on micro vacuum pump industry - market status analysis and development prospect prediction
- Structured text language XML
- MySQL learning notes (Advanced)
- Research Report on plastic antioxidant industry - market status analysis and development prospect forecast
- pip安装whl文件报错:ERROR: ... is not a supported wheel on this platform
- Construction and maintenance of business website [1]
- The web version of xshell supports FTP connection and SFTP connection [detailed tutorial] continued from the previous article
- CVPR论文解读 | 弱监督的高保真服饰模特生成
猜你喜欢
Today, I met a Alipay and took out 35K. It's really sandpaper to wipe my ass. it's a show for me
基本IO接口技术——微机第七章笔记
[hands on deep learning]02 softmax regression
关于测试用例
Redis distributed lock failure, I can't help but want to burst
[dynamic planning] p1220: interval DP: turn off the street lights
Baidu sued a company called "Ciba screen"
PIP audit: a powerful security vulnerability scanning tool
MySQL learning record (9)
pip安装whl文件报错:ERROR: ... is not a supported wheel on this platform
随机推荐
[shutter] shutter layout component (fractionallysizedbox component | stack layout component | positioned component)
[CV] Wu Enda machine learning course notes | Chapter 12
How is LinkedList added?
kernel tty_ struct
pip安装whl文件报错:ERROR: ... is not a supported wheel on this platform
Pyqt picture decodes and encodes and loads pictures
Three chess games
Analysis of neural network
How to prevent your jar from being decompiled?
Common routines of compressed packets in CTF
beginning
如何访问kubernetes API?
【零基础一】Navicat下载链接
【剑指 Offer】56 - I. 数组中数字出现的次数
Plastic floating dock Industry Research Report - market status analysis and development prospect forecast
Check the confession items of 6 yyds
Redis distributed lock failure, I can't help but want to burst
Golang string segmentation
MySQL learning record (8)
China microporous membrane filtration market trend report, technological innovation and market forecast