惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
The GitHub Blog
The GitHub Blog
美团技术团队
Security Latest
Security Latest
The Cloudflare Blog
D
DataBreaches.Net
Microsoft Azure Blog
Microsoft Azure Blog
V
Visual Studio Blog
宝玉的分享
宝玉的分享
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Exploit Database - CXSecurity.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
P
Proofpoint News Feed
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Security Affairs
腾讯CDC
H
Hacker News: Front Page
Microsoft Security Blog
Microsoft Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 三生石上(FineUI控件)
C
Check Point Blog
S
Security @ Cisco Blogs
Google DeepMind News
Google DeepMind News
Cisco Talos Blog
Cisco Talos Blog
云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Security Archives - TechRepublic
Security Archives - TechRepublic
I
InfoQ
量子位
Apple Machine Learning Research
Apple Machine Learning Research
AWS News Blog
AWS News Blog
TaoSecurity Blog
TaoSecurity Blog
月光博客
月光博客
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
aimingoo的专栏
aimingoo的专栏
S
SegmentFault 最新的问题
A
Arctic Wolf
Hacker News: Ask HN
Hacker News: Ask HN
D
Docker
Hugging Face - Blog
Hugging Face - Blog
I
Intezer
雷峰网
雷峰网
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
SecWiki News
SecWiki News
N
News and Events Feed by Topic

博客园 - emanlee

ubuntu 设置北京日期时间 mysql 在新电脑上导入,出现错误:ERROR at line 4019: Unknown command '\''. - emanlee 安装 yarn npm : 无法加载文件 C:\Program Files\nodejs\npm.ps1,因为在此系统上禁止运行脚本。 - emanlee VS code, 最近打开过哪些项目或文件夹,把这些备份下来,到另外一台电脑上恢复 傲梅分区助手 【好用】 ubuntu 可以使用Windows远程桌面连接吗?【可以】 把xshell的全部连接和密码,从一台Windows 10电脑,迁移到Windows11电脑。 Windows 远程桌面(mstsc)完整迁移方案(含历史连接记录、RDP 配置、保存的账号密码)【可行】 Endnote,如何能完整迁移库和模版 ubuntu 安装 locate “华为杯”第八届中国研究生人工智能创新大赛 当数据量很大时,执行 mysql> source filename.sql 很慢,有什么办法更快 pip install django-ranged-response==0.2.0 部署Vue+Django 04c,mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory Ubuntu Server 24.04 安装 MySQL, ./mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory ubuntu server 24.04: 如何设置默认采用 Xorg 方式登录 Ubuntu Server 24.04 设置锁屏时间 Ubuntu Server 启动过程中,比较慢 Job systemd-networkd-wait-online.service/start running Ubuntu Server 24.04 启用root用户 Ubuntu snap install 安装todesk是否必须检查 CPU 是否支持 AVX / AVX2 命令 Ubuntu Server 24.04下解决SunloginClient 向日葵依赖libgconf-2-4安装问题 Ubuntu Server 24.04 安装图形界面 ubuntu server 24.04 安装 中文输入法 Ubuntu Server 24.04 安装浏览器firefox Ubuntu Server 24.04 安装todesk【先联网,CPU必须支持 AVX / AVX2 】 Ubuntu Server 24.04 安装中文,汉字 Python赋值,什么时候传引用? 大驼峰命名法 python 容易理解类和对象的例子 django+mysql: 如何添加一个新的超级用户? Python 异常体系核心:BaseException 及其四大子类 Python 匿名函数(lambda)举例 Python函数内部的局部变量和全局变量 订书钉规格 在Navicat中复制带有自动增长id字段的表格,如何把新表格的自动增长id重新设置为从1开始 科研02 期刊和会议的质量等级 汉明距离(Hamming distance) Modbus slave模拟的从站 如何主动发送数据给主站 Word中,表格中的文字在一行的偏下方,没有垂直居中 在线把汉字转拼音字母 Navicat中复制并修改MySQL表,保存时,出现未知的列的错误 WinSCP 便携版、绿色版 下载 centos / Linux 上,非管理员用户安装 miniconda FTP 服务器设置(FileZilla Server) Windows主机上的Device/Credential Guard功能与VMware Workstation虚拟化冲突导致 Navicat 备份连接
Vue 2,父组件中点击一个按钮,更新子组件(重新加载子组件)
emanlee · 2026-01-28 · via 博客园 - emanlee

Vue 2,父组件中点击一个按钮,更新子组件(重新加载子组件)【可行】

想要在 Vue 2 中通过点击父组件的按钮,实现子组件的重新加载(重新渲染、执行完整生命周期),这个需求可以通过 Vue 核心的 key 属性来优雅实现,这也是 Vue 官方推荐的最佳方式。

实现思路

Vue 的虚拟 DOM 对比算法会根据 key 属性识别组件实例,当绑定在子组件上的 key 值发生变化时,Vue 会销毁旧的子组件实例、创建新的实例,从而触发子组件的完整生命周期(created/mounted 等),达到 “重新加载” 的效果

完整代码示例

1. 父组件(Parent.vue)

<template>
  <div class="parent">
    <!-- 点击按钮触发子组件重新加载 -->
    <button @click="reloadChildComponent" class="btn">重新加载子组件</button>
    <!-- 给子组件绑定 key,key 变化则子组件重新渲染 -->
    <ChildComponent :key="childKey" />
  </div>
</template>

<script>
// 引入子组件
import ChildComponent from './ChildComponent.vue'

export default {
  name: 'ParentComponent',
  components: {
    ChildComponent
  },
  data() {
    return {
      // 用于控制子组件重新加载的 key 值
      childKey: 0
    }
  },
  methods: {
    // 点击按钮更新 key 值
    reloadChildComponent() {
      // 方式1:自增(简单且易追踪)
      this.childKey += 1
      // 方式2:随机数(适合不需要追踪 key 数值的场景)
      // this.childKey = Math.random()
    }
  }
}
</script>

<style scoped>
.btn {
  padding: 8px 16px;
  cursor: pointer;
  margin-bottom: 20px;
}
</style>

2. 子组件(ChildComponent.vue)

<template>
  <div class="child">
    <h4>我是子组件</h4>
    <p>当前子组件实例的 key:{{ $vnode.key }}</p>
    <p>加载时间:{{ loadTime }}</p>
  </div>
</template>

<script>
export default {
  name: 'ChildComponent',
  data() {
    return {
      loadTime: ''
    }
  },
  // 生命周期钩子验证重新加载
  created() {
    console.log('子组件 created 钩子执行(新实例创建)')
    this.loadTime = new Date().toLocaleTimeString()
  },
  mounted() {
    console.log('子组件 mounted 钩子执行(DOM 挂载完成)')
  },
  destroyed() {
    console.log('子组件 destroyed 钩子执行(旧实例销毁)')
  }
}
</script>

关键代码解释

  1. key 属性绑定:父组件给子组件绑定 :key="childKey",这是触发子组件重新加载的核心
  2. 更新 key 值:点击按钮时执行 reloadChildComponent 方法,通过 this.childKey += 1 改变 key 值(也可以用随机数、时间戳等)。
  3. 生命周期验证:子组件中添加了 created/mounted/destroyed 钩子,点击按钮后可以在控制台看到旧实例销毁、新实例创建的日志,同时页面上的 “加载时间” 会更新,证明子组件已重新加载。

补充说明

  • 对比 v-if 方案:虽然 v-if="false" 再设为 true 也能实现重新加载【不行】,但 key 方案更简洁(无需控制显示隐藏)、更符合 Vue 设计理念,尤其适合仅需 “重新加载” 而非 “显隐切换” 的场景
  • 传参场景适配:如果父组件给子组件传递 props,重新加载后的子组件会自动接收最新的 props 值,无需额外处理。

总结

  1. 核心方案:给子组件绑定 key 属性,通过更新 key 值触发子组件重新渲染。
  2. 实现逻辑:key 值变化时,Vue 销毁旧子组件实例并创建新实例,子组件完整生命周期重新执行。
  3. 优势:简洁、符合 Vue 最佳实践,适配绝大多数 “重新加载子组件” 的业务场景。