Vue3.0 使用Teleport组件把对话框渲染的到指定位置

Teleport 提供了一种干净的方法,允许我们控制在 DOM 中哪个父节点下渲染了 HTML,而不必求助于全局状态或将其拆分为两个组件。

效果:

目录:

index.html:

<!DOCTYPE html>
<html lang="">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    <title><%= htmlWebpackPlugin.options.title %></title>
  </head>
  <body>
    <noscript>
      <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <div id="app"></div>
    <div id="m-dialog"></div>
    <!-- built files will be auto injected -->
  </body>
</html>

Dialog.vue:

<template>
  <div :class="[`m-mask ${visible ? 'active' : ''}`]">
    <div class="m-dialog">
      <div class="m-header">
        <span class="m-header-title">{{title}}</span>
        <span class="m-header-close" @click="handleClose">X</span>
      </div>
      <div class="m-content">
        <slot name="content"></slot>
      </div>
      <div class="m-footer">
        <button class="m-btn" @click="handleOk">确定</button>
        <button class="m-btn" @click="handleClose">取消</button>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  props: {
    visible: {
      type: Boolean
    },
    title: {
      type: String
    }
  },
  emits: ['onClose', 'onOk'],
  setup(props, context) {

    const handleClose = () => {
      context.emit('onClose')
    }

    const handleOk = () => {
      context.emit('onOk')
    }

    return {
      handleClose,
      handleOk
    }
  }
}
</script>

<style>

</style>

Home.vue:

<template>
  <Teleport to="#m-dialog">
    <Dialog :visible="visible" title="标题" @onClose="handleCloseDialog" @onOk="handleOk">
      <template #content>
        <div class="m-info">
          <span class="m-msg">你好!</span>
        </div>
      </template>
    </Dialog>
  </Teleport>
  <button @click="handleShowDialog">按钮</button>
</template>

<script>
import { reactive, toRefs } from "vue";
import Dialog from "../components/Dialog";

export default {
  components: {
    Dialog,
  },
  setup() {
    const state = reactive({
      visible: false,
    });

    const handleShowDialog = () => {
      state.visible = true
    }

    const handleCloseDialog = () => {
      state.visible = false
    }

    const handleOk = () => {
      console.log('ok')
      handleCloseDialog()
    }

    const { visible } = toRefs(state)

    return {
      visible,
      handleShowDialog,
      handleCloseDialog,
      handleOk
    }
  },
};
</script>

<style>
</style>

index.css:

.m-mask{position: absolute;display: none; top: 0;left: 0;right: 0;bottom: 0;background: rgba(0 , 0, 0, 0.1);}
.m-mask.active{display: flex;}
.m-dialog{display: flex;flex-direction: column; margin: auto;min-width: 400px;min-height: 100px;background: #fff;border-radius: 5px;box-shadow: 10px 10px 5px #888;}
.m-header{display: flex;line-height: 40px;border-bottom: 1px solid #ddd;}
.m-header-title{flex:1;padding: 0 0 0 5px;}
.m-header-close{width: 40px;text-align: center;cursor: pointer;}
.m-info{display: flex;height: 100px;}
.m-msg{margin: auto;}
.m-content{flex:1;}
.m-footer{ line-height: 40px;border-top: 1px solid #ddd; text-align: right;}
.m-btn{margin: 0 5px;}


 

 

 

 

 

 

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐同保

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值