当前位置:网站首页>在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
边栏推荐
- MySQL installation failed -gpg verification failed
- Analysis of enterprise financial statements [4]
- Accounting regulations and professional ethics [19]
- System (hierarchical) clustering method and SPSS implementation
- Spend more time with your computer on this special holiday, HHH
- The web version of xshell supports FTP connection and SFTP connection [detailed tutorial] continued from the previous article
- 【零基础一】Navicat下载链接
- Go web programming practice (1) -- basic syntax of go language
- Plastic granule Industry Research Report - market status analysis and development prospect forecast
- 读博士吧,研究奶牛的那种!鲁汶大学 Livestock Technology 组博士招生,牛奶质量监测...
猜你喜欢
如何防止你的 jar 被反编译?
GEE:(二)对影像进行重采样
Go language learning summary (5) -- Summary of go learning notes
tinymce可视化编辑器增加百度地图插件
[shutter] shutter page Jump (route | navigator | page close)
MySQL learning record (5)
[shutter] statefulwidget component (create statefulwidget component | materialapp component | scaffold component)
Common routines of compressed packets in CTF
[shutter] statefulwidget component (bottom navigation bar component | bottomnavigationbar component | bottomnavigationbaritem component | tab switching)
[dynamic planning] p1220: interval DP: turn off the street lights
随机推荐
~90z axis translation
MySQL learning record (6)
MySQL learning record (5)
MySQL learning record (7)
Analysis of enterprise financial statements [1]
[CV] Wu Enda machine learning course notes | Chapter 12
Structure array, pointer and function and application cases
MySQL learning record (1)
[shutter] shutter layout component (Introduction to layout component | row component | column component | sizedbox component | clipoval component)
MySQL learning record (9)
3DES (deSede) encryption CBC mode pkcs7padding filling Base64 encoding key 24byte iv8byte
*C语言期末课程设计*——通讯录管理系统(完整项目+源代码+详细注释)
Cardinality sorting (detailed illustration)
如何防止你的 jar 被反编译?
Import a large amount of data to redis in shell mode
Physical layer cables and equipment
China Indonesia advanced wound care market trend report, technological innovation and market forecast
B.Odd Swap Sort(Codeforces Round #771 (Div. 2))
China's log saw blade market trend report, technological innovation and market forecast
CVPR论文解读 | 弱监督的高保真服饰模特生成