簡(jiǎn)述
<Teleport> 是一個(gè)內(nèi)置組件,它可以將一個(gè)組件內(nèi)部的一部分模板“傳送”到該組件的 DOM 結(jié)構(gòu)外層的位置去。
- 注意
<Teleport> 掛載時(shí),傳送的 to 目標(biāo)必須已經(jīng)存在于 DOM 中。理想情況下,這應(yīng)該是整個(gè) Vue 應(yīng)用 DOM 樹(shù)外部的一個(gè)元素。如果目標(biāo)元素也是由 Vue 渲染的,你需要確保在掛載 <Teleport> 之前先掛載該元素。 - 場(chǎng)景
在項(xiàng)目中如需要全局彈框,使用此組件 - 使用
<button @click="open = true">Open Modal</button>
<Teleport to="body">
<div v-if="open" class="modal">
<p>Hello from the modal!</p>
<button @click="open = false">Close</button>
</div>
</Teleport>