瞬间移动组件(Teleport)

34 篇文章 0 订阅
Teleport 是一种能够将我们的模板移动到 DOM 中 Vue app 之外的其他位置的技术。

如果我们嵌套在 Vue app 内的某个组件内部,那么处理嵌套组件的定位、z-index 和样式就会变得很困难。
使用 Teleport 就可以方便的解决组件间 css 层级问题

我们将模态内容包装在 teleport 组件中,
还需要指定一个 to 属性,为该属性分配一个查询选择器,以标识目标元素。

父组件

<template>
  <div id="d1">
    <h3>第一个div</h3>
  </div>
  <div id="d2">
    <h3>第二个div</h3>
  </div>
  <ModalButton></ModalButton>
</template>

<script lang='ts'>
import { reactive, toRefs, onBeforeMount, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'//引入路由
import ModalButton from "../components/1.vue";
export default {
  name: '',
  components: {
    ModalButton
  },
  setup() {
    let router = useRouter(), route = useRoute();
    const data = reactive({
    })
    onBeforeMount(() => {
    })
    onMounted(() => {
    })
    const refData = toRefs(data);
    return {
      ...refData,
    }

  }
};
</script>
<style scoped>
.modal {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.modal div {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-around;
  background-color: white;
  width: 350px;
  height: 300px;
  padding: 5px;
}
</style>

子组件

<template>
    <div>
        <button @click="modalOpen = true">弹出一个模态框</button>
        <teleport to="body">
            <div v-if="modalOpen" class="modal">
                <div>
                    这是一个弹框,挂载在 body 元素上。
                    <button @click="modalOpen = false">关闭</button>
                </div>
            </div>
        </teleport>

    </div>
</template>

<script lang='ts'>
import { reactive, toRefs, onBeforeMount, onMounted, defineComponent } from 'vue'
import { useRouter, useRoute } from 'vue-router'//引入路由
export default
    {
        name: '',
        setup() {
            let router = useRouter(), route = useRoute();
            const data = reactive({
                modalOpen: false
            })
            onBeforeMount(() => {
            })
            onMounted(() => {
            })
            const refData = toRefs(data);
            return {
                ...refData,
            }

        }
    };
</script>
<style scoped>
.modal {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

.modal div {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-around;
    background-color: white;
    width: 350px;
    height: 300px;
    padding: 5px;
}
</style>
  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值