teleport 远距离传送
英[ˈtelipɔːt]
美[ˈtelipɔːrt]

笔记

数据驱动显示。

视频内容

  • 什么是Teleport?—— Teleport 是一种能够将我们的组件html结构移动到 指定位置 的技术。

    <teleport to="移动位置">
        <div v-if="isShow" class="mask">
            <div class="dialog">
                <h3>我是一个弹窗</h3>
                <button @click="isShow = false">关闭弹窗</button>
            </div>
        </div>
    </teleport>

源码

components\Dialog.vue

<template>
    <div>
        <button @click="isShow = true">点我弹个窗</button>

        <!-- 把想弹出来的结构用 teleport 包裹起来 -->
        <teleport to="body">
            <div v-if="isShow" class="mask">
                <div class="dialog">
                    <h3>我是一个弹窗</h3>
                    <h4>一些内容</h4>
                    <h4>一些内容</h4>
                    <h4>一些内容</h4>
                    <button @click="isShow = false">关闭弹窗</button>
                </div>
            </div>
        </teleport>

    </div>
</template>

<script>
    import {ref} from 'vue'
    export default {
        name:'Dialog',
        setup(){
            let isShow = ref(false)
            return {isShow}
        }
    }
</script>

<style>
    .mask{
        position: absolute;
        top: 0;bottom: 0;left: 0;right: 0;
        background-color: rgba(0, 0, 0, 0.5);
    }
    .dialog{
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
        text-align: center;
        width: 300px;
        height: 300px;
        background-color: green;
    }
</style>