Vue3学习:Teleport组件-新的组件

Teleport:瞬间移动

————-teleport 是一种能够将我们的组件html结构 移动到指定位置的技术

祖先组件:

<template>
    <div class="app">
        <h3>我是App组件</h3>
        <Child/>
    </div>
</template>

<script>
    import Child from './components/Child'
    export default {
        name:'App',
        components:{Child},//使用子
    }
</script>

<style>
    .app{
        background-color: gray;
        padding: 10px;
    }
</style>

子组件

<template>
    <div class="child">
        <h3>我是Child组件</h3>
        <Son/>
    </div>
</template>

<script>
    import Son from './Son'
    export default {
        name:'Child',
        components:{Son},
    }
</script>

<style>
    .child{
        background-color: skyblue;
        padding: 10px;
    }
</style>

孙组件——使用了点击弹窗组件

<template>
    <div class="son">
        <h3>我是Son组件</h3>
        <Dialog/>
    </div>
</template>

<script>
    import Dialog from './Dialog.vue'
    export default {
        name:'Son',
        components:{Dialog}
    }
</script>

<style>
    .son{
        background-color: orange;
        padding: 10px;
    }
</style>

弹窗组件v-if会移除DOM结构 ,如何使得需要弹窗在页面中间

<template>
    <div>
        <button @click="isShow = true">点我弹个窗</button>
        
            <div v-if="isShow">
                <div class="dialog">
                    <h3>我是一个弹窗</h3>
                    <h4>一些内容</h4>
                    <h4>一些内容</h4>
                    <h4>一些内容</h4>
                    <button @click="isShow = false">关闭弹窗</button>
                </div>
            </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-color: green;
    }
</style>

需要弹窗走出页面:

<teleport to="body"> 传送走

<template>
    <div>
        <button @click="isShow = true">点我弹个窗</button>
        <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>

直接相对于body页面了

整个屏幕遮罩层

    .mask{
        position: absolute;
        top: 0;bottom: 0;left: 0;right: 0;
        background-color: rgba(0, 0, 0, 0.5);
    }
<template>
    <div>
        <button @click="isShow = true">点我弹个窗</button>
        <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>

也可以传送到其他位置app2

<teleport to="#app2">

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值