You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

挂载于body的Teleport子组件,父组件卸载后是否仍可用?

问题解答

当TheFirstComponent卸载(弹窗关闭)后,Teleport中的TheSecondComponent无法再使用。

核心原因在于:Teleport仅仅是改变了组件DOM节点的挂载位置,将其从父组件的DOM树转移到body节点下,但组件的实例生命周期仍然完全由它的父组件(TheFirstComponent)管控。

当TheFirstComponent被卸载时,Vue会递归销毁它的所有子组件实例,包括被Teleport包裹的TheSecondComponent:

  • TheSecondComponent对应的DOM元素会从body中移除
  • 组件实例的unmounted生命周期钩子会触发
  • 组件的响应式数据、事件监听等都会被清理,无法再进行正常交互

你的代码示例(格式化后):

<!-- TheFirstComponent -->
<template>
  <Teleport to="body">
    <TheSecondComponent />
  </Teleport>
</template>

内容的提问来源于stack exchange,提问作者pepeevich

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 06:52:00