美文网首页
Vue3.0新特性----teleport组件

Vue3.0新特性----teleport组件

作者: 有一种感动叫做丶只有你懂 | 来源:发表于2021-03-12 16:48 被阅读0次

teleport

  • 官方文档传送门
  • 俗称传送门组件,是一个全局组件
  • 使用场景,比如我子组件里有一个弹窗modal,我希望这个弹窗被渲染的时候挂到body上面,而不是当前的子组件
  • to表示的是一个选择器

子组件ModalButton.vue

<template>
  <button @click="modalOpen">展示模态框</button>
  <teleport to="body">
    <div v-if="visiable">
      <div class="box">
        <div class="content">
          这是一个消息
          <button @click="modalClose">点击关闭</button>
        </div>
      </div>
    </div>
  </teleport>
</template>
<style scoped>
.box {
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
  position: absolute;
  top: 0;
  right: 0;
}
.content {
  width: 400px;
  height: 400px;
  background: white;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
</style>
<script lang="ts">
import Vue, {defineComponent} from "vue";
export default defineComponent({
  data() {
    return {
      visiable: false,
    };
  },
  methods: {
    modalOpen() {
      this.visiable = true;
    },
    modalClose() {
      this.visiable = false;
    },
  },
});
</script

父组件Teleport.vue

<template>
  <h1>Teleport.vue</h1>
  <modal-button></modal-button>
</template>
<script lang="ts">
import Vue, {defineComponent} from "vue";

import ModalButton from "@/components/ModalButton.vue";
export default defineComponent({
  components: {
    ModalButton,
  },
  data() {
    return {};
  },
});
</script>

看一下结果

image.png
被渲染到了body元素中,而不是当前的子组件

相关文章

  • Vue3.0新特性----teleport组件

    teleport 官方文档传送门[https://v3.cn.vuejs.org/api/built-in-com...

  • vue3.0新特性teleport

    比如自己写个modal组件时,挂载到body上最好的选择,可以通过z-idnex来控制层级。但是又想在引用moda...

  • VUE3.0 teleport组件

    teleport 组件 也可以说独立组件 绑定

  • Vue 3.0 Teleport的使用和原理分析

    Vue3.0 新增了一个Teleport组件,开发者可以使用它将其所在组件模板的部分内容移动到特定的DOM位置,譬...

  • Vue3_19(Teleport传送组件)

    Teleport Vue 3.0新特性之一。Teleport 是一种能够将我们的模板渲染至指定DOM节点,不受父级...

  • Vue3新特性:内置组件teleport

    Vue3内置组件teleport 将teleport包裹的元素移动到某个DOM节点下有两个参数to - strin...

  • 九.弹出模态框使用Teleport

    使用Teleport实现一个模态对话框的组件 teleport ,把模板的内容移动到当前组件之外的DOM 中,可以...

  • Teleport组件

    Teleport也被称为传送门组件,用于提供一种简洁的方式,指定其里面内容的父元素 为了方便我将相关内容简记如下图...

  • Teleport组件

    Teleport是个内置组件,它可以将组件内部模板传送到DOM结构外层位置去 使用 一般用于模态框

  • Vue3.0 新特性

    Vue3.0 在去年9月正式发布了,也有许多小伙伴都热情的拥抱Vue3.0,这篇文章就是在使用后的一个总结, 包含...

网友评论

      本文标题:Vue3.0新特性----teleport组件

      本文链接:https://www.haomeiwen.com/subject/lxmgqltx.html