vue3.0 Teleport组件

准备三个组件

<template>
  <div class="app">
    <h1>我是App组件</h1>
    <chlid></chlid>
  </div>
</template>

<script>
import { provide, reactive, toRefs } from "vue";
import chlid from "@/components/child.vue";
export default {
  components: {
    chlid,
  },
};
</script>

<style scoped>
.app {
  background: pink;
  padding: 20px;
}
</style>
<template>
  <div class="child">
    <h2>我是child组件</h2>
    <son></son>
  </div>
</template>

<script>
import { reactive, toRefs } from "vue";
import son from "@/components/son.vue";
export default {
  name: "chlid",
  components: {
    son,
  },
};
</script>

<style scoped>
.child {
  background: red;
  padding: 20px;
}
</style>
<template>
  <div class="son">
    <h3>我是孙组件</h3>
  </div>
</template>

<script>
import { reactive, toRefs } from "vue";
export default {
  name: "son",
};
</script>

<style scoped>
.son {
  background: yellow;
  padding: 20px;
}
</style>

实现一个需求
在这里插入图片描述
写一个简单的dialog组件

<template>
  <div>
    <button @click="isShow = true">弹窗</button>
    <div class="dialog" v-if="isShow">
      <h3>弹窗</h3>
      <h4>内容</h4>
      <h4>内容</h4>
      <h4>内容</h4>
      <button @click="isShow = false">关闭弹窗</button>
    </div>
  </div>
</template>
<script>
import { ref } from "vue";
export default {
  name: "dialog",
  setup() {
    let isShow = ref(false);
    return {
      isShow,
    };
  },
};
</script>
<style>
.dialog {
  width: 300px;
  height: 300px;
  background: blue;
}
</style>

在这里插入图片描述
使用时会发现,父组件被撑开了
在这里插入图片描述
可以看到,弹窗在son里面
在这里插入图片描述

应用以下teleport

  <div>
    <button @click="isShow = true">弹窗</button>
    <teleport to="body">
      <div class="dialog" v-if="isShow">
        <h3>弹窗</h3>
        <h4>内容</h4>
        <h4>内容</h4>
        <h4>内容</h4>
        <button @click="isShow = false">关闭弹窗</button>
      </div>
    </teleport>
  </div>
</template>

在这里插入图片描述
结构也没有被撑开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值