其他分享
首页 > 其他分享> > Vue3.0(2):Teleport

Vue3.0(2):Teleport

作者:互联网

Teleport

官方文档

Teleport汉语意思是传送,从官方文档看这个名字很符合,可以将代码片段传送到其他位置

官方示例1(略作修改)

//不使用Teleport
<template>
  <div>
    <el-button type="primary" @click="openModel = true">打开</el-button>
    <div title="main">
      <div >
        <div
          v-show="openModel"
          style="width: 100px; height: 100px; background: red"
        >
          <el-button type="primary" @click="openModel = false">关闭</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

//使用Teleport
<template>
  <div>
    <el-button type="primary" @click="openModel = true">打开</el-button>
    <div title="main">
        <teleport to="body">
        <div
          v-show="openModel"
          style="width: 100px; height: 100px; background: red"
        >
          <el-button type="primary" @click="openModel = false">关闭</el-button>
        </div>
      </teleport>
      </div>
    </div>
  </div>
</template>

在这里插入图片描述
在这里插入图片描述

与 Vue components 一起使用

<teleport to="#tobody">
    <modal-button></modal-button>
 </teleport>

在同一目标上使用多个 teleport

<teleport to="#modals">
  <div>A</div>
</teleport>
<teleport to="#modals">
  <div>B</div>
</teleport>

<!-- result-->
<div id="modals">
  <div>A</div>
  <div>B</div>
</div>

标签:Teleport,teleport,元素,Vue3.0,使用,组件,挂载
来源: https://blog.csdn.net/weixin_41897680/article/details/118819523