vue3 teleport组件传送

父组件

<template>
  <div class="wrap">
    <div id="app" style="position: relative">
      <h3>Tooltips with Vue 3 Teleport</h3>
      <div>
        <modalButton></modalButton>
      </div>
    </div>
  </div>
</template>
<script>
import { onMounted, ref, toRaw } from "vue";
import modalButton from "./components/modalButton.vue";
export default {
  components: {
    modalButton,
  },
};
</script>
<style scoped>
.wrap {
  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden;
  /* border: 1px solid red; */
}
</style>

子组件

子组件可通过teleport,传送到页面中任一dom中,teleport标签中的to属性值和css选择器规则相同,例如to="#id", to=".class",to="body"等。

<template>
  <div class="wrap">
    <button @click="modalOpen = true">
      Open full screen modal! (With teleport!)
    </button>

    <teleport to="body">
      <div v-if="modalOpen" class="modal">
        <div>
          I'm a teleported modal! (My parent is "body")
          <button @click="modalOpen = false">Close</button>
        </div>
      </div>
    </teleport>
  </div>
</template>
<script setup>
import { onMounted, ref, toRaw } from "vue";
let modalOpen = ref(false);
onMounted(() => {});
</script>
<style scoped>
.wrap {
  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden;
}
</style>
posted @   微山湖上静悄悄  阅读(64)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
Document
点击右上角即可分享
微信分享提示