vue3 teleport的使用
## 認識Teleport
#### 某些情況下,我們希望組件不是掛載在這個組件樹上的,可能是移動到Vue app之外的其他位置
##### 我們可以借它之手轉(zhuǎn)移到指定元素內(nèi)部
#### Teleport是什么呢?
##### 它是一個Vue提供的內(nèi)置組件,類似于react的Portals,理解為聯(lián)盟里的tp
#### 它有兩個屬性:
##### to:指定將其中的內(nèi)容移動到的目標(biāo)元素,可以使用選擇器
##### disabled:是否禁用teleport 的功能
<template>
? ?<div>
? ? ? ?<teleport to="body">
? ? ? ? ? ?<h2>我被轉(zhuǎn)移啦</h2>
? ? ? ?</teleport>
? ?</div>
</template>
#### 我們可以在teleport 中使用組件,并且也可以給他傳入一些數(shù)據(jù)
<template>
? ?<div>
? ? ? ?<teleport to="body">
? ? ? ? ? <component message="hello teleport 當(dāng)前組件內(nèi)數(shù)據(jù)"></component>
? ? ? ?</teleport>
? ?</div>
</template>



標(biāo)簽: