Vue3之对Dialog的简单封装


    之前使用的UI框架,无论是Element UI/Plus 还是 Ant design,其中Dialog组件中的结构和样式都难以修改,无论是使用less、deep还是其他方法,对其组件中css的修改都不生效(不确定是否有其他解决方法),所以我就自己简单封装了一个Dialog组件,只是简单实现了遮蔽层以及Dialog外框,内在具体实现,例如Header、Main、Footer的css和其他内容全部交给程序员决定。

以下是Dialog组件代码

<template>
  <teleport to="body" v-if="visible">
    <div class="box">
      <div class="dialog">
      
        <slot name="header">
          <div class="header">
            <template v-if="title">
              {{title}}
            </template>
            
            <!-- 下面这行代码是关闭按钮的icon图标,即形似 X 的图标,以下代码来自于Element Plus Icon库,若没有引入Element Plus Icon库,建议相应进行修改 -->
            <el-icon class="closeIcon"
                     @click.stop="closeDialog"
                     color="grey">
             <Close />
             
            </el-icon>
          </div>
        </slot>

        <!--default slot-->
        <slot ></slot>

        <slot name="footer">

        </slot>
      </div>
    </div>
  </teleport>
</template>

<script>
export default {
  name: "Dialog",
  props:{
    visible:{
      type:Boolean,
      required:true
    },
    height:{
      type:String,
      required: false,
      default:'400px'
    },
    width:{
      type:String,
      required:false,
      default: '500px'
    },
    title:{
      type:String,
      required:false
    }
  },
  setup(pros,context){
    //关闭Dialog
    const closeDialog=()=>{
     context.emit('update:visible',false);
    };
    return{
      closeDialog
    }
  }
}
</script>

<style scoped>
.box{
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background-color:rgba(0,0,0,0.5);
}
.dialog{
  position: absolute;
  left: 50%;
  top:50%;
  transform: translate(-50%,-50%);
  height: v-bind(height);
  width: v-bind(width);
  background-color: white;
}
.header{
  --height: 50px;
  height: var(--height);
  background-color: #ced6e0;
  line-height: var(--height);
  font-size:22px;
  letter-spacing: 10px;
  text-align: center;
  box-shadow: 0 2px 10px 0 rgb(0 0 0 / 10%);
}
.closeIcon{
  position: absolute;
  right: 6px;
  top: 6px;
  cursor: pointer;
}
</style>

以上代码使用了Vue3的teleport标签,用于之间将teleport标签中的html结构将指定body标签中。
使用了visible属性控制Dialog的存在与消亡,剩余的title属性控制组件标题,不写则不显示标题,heightwidth分别控制组件高度和宽度,不写则使用默认值,这两个值都是String类型,使用500px50vh50em等单位长度都能成功赋值

组件的使用方式: <Dialog v-model:visible="visible" > </Dialog>
使用v-model:visible使父子组件双向绑定visible属性的值,在Vue2中,则是 v-model:asyn写法

<Dialog v-model:visible="visible" > </Dialog>内编写html结构要点:

  • <template v-slot:header > Here is header </template>
    代码中编写组件Header内容,包括标题和关闭图标都需要自己编写,如果不使用header插槽,则使用默认的样式结构
  • <template v-slot:default> Here is default </template>
    代码中编写组件的主体内容,可以省略这段代码直接写在外层
  • <template v-slot:footer> Here is footer </template>
    代码中编写组件的尾部内容,主要是编写确定和取消按钮

测试代码一

<template>
  <Dialog v-model:visible="visible" >
    <template v-slot:header>
      <div>
        123
      </div>
    </template>
    
    <template v-slot:default>
      <div>
        123
      </div>
    </template>
    
    <template v-slot:footer>
      <div>
        123
      </div>
    </template>
    
  </Dialog>
</template>

<script>
import Dialog from "@/components/Dialog";
import {ref} from "vue";

export default {
  name: "Test",
  components: {Dialog},
  setup(){
    const visible=ref(true);
    return{
      visible
    }
  }
}
</script>

<style scoped>

</style>

在这里插入图片描述

测试代码二

  <Dialog v-model:visible="visible" title="设置" height="400px" width="450px">
    Hello World
  </Dialog>

在这里插入图片描述

以上代码仅仅是简单的封装,若是想向外暴露控制更多的样式,请自行动态绑定class或者使用v-bind()动态赋予样式属性值。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据你提供的引用内容,可以看出你想要了解如何在Vue3中封装el-dialog组件。 在Vue3中,你可以自己封装一个类似el-dialog的组件。你可以通过使用`<LZDialog>`组件来达到这个目的。在你的代码中,`<LZDialog>`组件具有以下属性和方法: 属性: - `dialogVisible`:控制弹窗是否可见 - `title`:弹窗标题 - `width`:弹窗宽度 - `successBtnText`:确定按钮文本 - `closeBtnText`:取消按钮文本 - `isFooter`:是否显示按钮 方法: - `dialogCloseDef`:关闭弹窗的方法 - `dialogSuccessDef`:确定按钮点击后的方法 你可以根据需要在`dialogCloseDef`和`dialogSuccessDef`方法中处理关闭弹窗和确定按钮点击后的逻辑。 另外,根据你提供的第二个引用内容,可以看出你还需要将`<LZDialog>`组件注册为全局组件,以便在项目中使用。你可以使用`install`方法来实现这个注册。 总结起来,在Vue3中封装el-dialog类似的组件的方法可以参考以下步骤: 1. 创建一个新的组件,例如`<LZDialog>`组件。 2. 在组件中定义所需的属性和方法,例如`dialogVisible`、`title`、`width`等属性,以及`dialogCloseDef`、`dialogSuccessDef`等方法。 3. 在组件中设置模板,包括弹窗的结构和样式。 4. 在需要使用这个组件的地方,通过`<LZDialog>`标签引入并使用。 5. 在项目的入口文件中,通过`install`方法将`<LZDialog>`组件注册为全局组件,以便在整个项目中使用。 希望这个答案能够帮助到你!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [vue3.x + elementPlus 封装组件之dialog弹框封装篇](https://blog.csdn.net/m0_62015496/article/details/125414348)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Vue3之对Dialog简单封装](https://blog.csdn.net/qq_16525829/article/details/128651551)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值