当前位置:网站首页>通过setTimeout解决子组件不会销毁的问题
通过setTimeout解决子组件不会销毁的问题
2022-06-28 15:58:00 【Attacking-Coder】
项目描述
代码参数说明
this.preview=true/false 来控制子组件是否渲染出来
this.previewUrl 来控制新渲染出来的自组建中图片的url
private handleItemPreviewClick(url) {
this.showPreview = false;
setTimeout(() => {
this.previewUrl = url;
this.showPreview = true;
}, 0);
}
// 子组件的生命周期函数
public destroyed(): void {
console.log('销毁');
}
此时当我们触发父组件的handleItemPreviewClick函数时子组件会进行销毁
这是因为:当我们执行完this.showPreview = false; setTimeout中的代码会放到宏任务队列中,当页面重新渲染完才会执行里边的代码
错误代码案例
private handleItemPreviewClick(url) {
this.showPreview = false;
this.previewUrl = url;
this.showPreview = true;
}
// 子组件的生命周期函数
public destroyed(): void {
console.log('销毁');
}
此时当我们触发父组件的handleItemPreviewClick函数时子组件
不会
进行销毁
这是因为:当我们执行完this.showPreview = false; 后立马又执行了this.showPreview = true;,当代码都执行完了 浏览器发现showPreview的值并没有变化,子组件也就不会被销毁了
边栏推荐
- Android, eclipse and MySQL upload pictures and get
- Gartner发布当前至2024年的五大隐私趋势
- 使用Karmada实现Helm应用的跨集群部署
- 【Proteus仿真】L297驱动步进电机
- A little hesitant in the morning
- 超自动化与网络安全的未来
- 征文投稿丨使用轻量应用服务器搭建博客环境
- The sadness of software testers is Their own technical ability can not meet the requirements of large manufacturers?
- 如何根据多元索引查询最后一条数据,达到 sql order by desc limit 1的效果呢?
- [recommendation system] esmm model of multi task learning (updating)
猜你喜欢
Visual Studio 2010 配置和使用Qt5.6.3
【Spock】处理 Non-ASCII characters in an identifier
有哪些好用的供应商管理系统
PostgreSQL enables grouping statistics by year, month, day, week, hour, minute and second
3. Caller 服务调用 - dapr
5分钟的时间制作一个反弹球游戏
Naacl 2022 | distillation of machinetranslation SOTA model
3. caller service call - dapr
Why MySQL table connection is faster than subquery
10:00面试,10:02就出来了 ,问的实在是太...
随机推荐
leetcode:22. bracket-generating
【Spock】处理 Non-ASCII characters in an identifier
Convolutional neural network for machine learning uses cifar10 data set and alexnet network model to train classification model, install labelimg, and report error
【高并发基础】MySQL索引优化
【MySQL】官网文档学习之查询语句sql注意事项
10: 00 interview, came out at 10:02, the question is really too
机器学习之卷积神经网络使用cifar10数据集和alexnet网络模型训练分类模型,安装labelimg,以及报错ERROR
成功迁移到云端需要采取的步骤
3. caller service call - dapr
一次简单的反射型XSS操作及思路
使用openpyxl操作Excel
媒体数据处理V2版本(VPC)图像缩放内容解析
[MySQL] official website document learning query statement SQL precautions
Azure Kinect Microsoft camera unity development summary
首次失败后,爱美客第二次冲刺港交所上市,财务负责人变动频繁
Vc2010 compilation qt5.6.3 prompt cvtres: fatal error cvt1107:
Tiktok actual battle ~ list of bloggers I follow, follow and check
Coding Devops helps Sinochem information to build a new generation of research efficiency platform and drive the new future of "online Sinochem"
Navicat 15 for MySQL
among us私服搭建