<Teleport>
<Teleport> 组件可以将组件传送到 DOM 中的不同位置。
<Teleport>
的 to
目标期望是一个 CSS 选择器字符串或一个实际的 DOM 节点。Nuxt 当前仅支持服务端渲染 (SSR) 将 teleport 发送到 #teleports
,对于其他目标则需使用 <ClientOnly>
包裹实现客户端支持。主体传送
<template>
<button @click="open = true">
打开模态框
</button>
<Teleport to="#teleports">
<div v-if="open" class="modal">
<p>来自模态框的问候!</p>
<button @click="open = false">
关闭
</button>
</div>
</Teleport>
</template>
客户端传送
<template>
<ClientOnly>
<Teleport to="#some-selector">
<!-- 内容 -->
</Teleport>
</ClientOnly>
</template>
Read and edit a live example in Docs > Examples > Advanced > Teleport.