前端开发攻略---在Vue3中对ElementPlus中的dialog组件进行二次封装

1、演示

2、子组件

component文件夹下面新建一个文件夹,我这里是myDialog,在 myDialog文件夹创建index.vue文件。

<template>
  <el-dialog
    v-model="visible"
    :title="title"
    :width="width"
    :fullscreen="fullscreen"
    :top="top"
    :modal="modal"
    :modal-class="modalClass"
    :append-to-body="appendToBody"
    :append-to="appendTo"
    :lock-scroll="lockScroll"
    :custom-class="customClass"
    :open-delay="openDelay"
    :close-delay="closeDelay"
    :close-on-click-modal="closeOnClickModal"
    :close-on-press-escape="closeOnPressEscape"
    :show-close="showClose"
    :before-close="beforeClose"
    :draggable="draggable"
    :overflow="overflow"
    :center="center"
    :align-center="alignCenter"
    :destroy-on-close="destroyOnClose"
    :close-icon="closeIcon"
    :z-index="ZIndex"
    :header-aria-level="headerAriaLevel"
  >
    <slot name="content"></slot>
    <template #footer>
      <span>
        <slot name="footer"></slot>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive } from 'vue'
const visible = ref(false)

const props = defineProps({
  // 标题
  title: { required: true, type: String, default: '' },
  // 宽度
  width: { required: false, type: String, default: '50%' },
  // 是否全屏
  fullscreen: { required: false, type: Boolean, default: false },
  // 距离屏幕顶部距离
  top: { required: false, type: String, default: '15vh' },
  // 是否需要遮罩层
  modal: { required: false, type: Boolean, default: true },
  // 遮罩的自定义类名
  modalClass: { required: false, type: String, default: '' },
  // Dialog 自身是否插入至 body 元素上。嵌套的 Dialog 必须指定该属性并赋值为 true
  appendToBody: { required: false, type: Boolean, default: false },
  // Dialog 挂载到哪个 DOM 元素 将覆盖 append-to-body
  appendTo: { required: false, type: String, default: 'body' },
  // 是否在 Dialog 出现时将 body 滚动锁定
  lockScroll: { required: false, type: Boolean, default: true },
  // Dialog 的自定义类名
  customClass: { required: false, type: String, default: '' },
  // dialog 打开的延时时间,单位毫秒
  openDelay: { required: false, type: Number, default: 0 },
  // dialog 关闭的延时时间,单位毫秒
  closeDelay: { required: false, type: Number, default: 0 },
  // 是否可以通过点击 modal 关闭 Dialog
  closeOnClickModal: { required: false, type: Boolean, default: true },
  // 是否可以通过按下 ESC 关闭 Dialog
  closeOnPressEscape: { required: false, type: Boolean, default: true },
  // 是否显示关闭按钮
  showClose: { required: false, type: Boolean, default: true },
  // 关闭前的回调,会暂停 Dialog 的关闭. 回调函数内执行 done 参数方法的时候才是真正关闭对话框的时候.
  beforeClose: { required: false, type: Function, default: () => {} },
  // 为 Dialog 启用可拖拽功能
  draggable: { required: false, type: Boolean, default: false },
  // 拖动范围可以超出可视区
  overflow: { required: false, type: Boolean, default: false },
  // 是否让 Dialog 的 header 和 footer 部分居中排列
  center: { required: false, type: Boolean, default: false },
  // 是否水平垂直对齐对话框
  alignCenter: { required: false, type: Boolean, default: false },
  // 当关闭 Dialog 时,销毁其中的元素
  destroyOnClose: { required: false, type: Boolean, default: false },
  // 自定义关闭图标,默认 Close
  closeIcon: { required: false, type: String, default: 'Close' },
  // 和原生的 CSS 的 z-index 相同,改变 z 轴的顺序
  ZIndex: { required: false, type: Number, default: 999999 },
  // header 的 aria-level 属性
  headerAriaLevel: { required: false, type: String, default: '2' },
})
// 关闭dialog的函数
const close = () => {
  visible.value = false
}
// 打开dialog的函数
const open = () => {
  visible.value = true
}

defineExpose({
  close,
  open,
})
</script>

<style scoped lang="scss"></style>

1、定义dialog原先自带的一些属性

2、使用defineExpose将子组件中控制dialog显示隐藏的方法抛出去

3、可以在这个子组件内任意修改样式

3、父组件

随便在哪个父组件里面使用

<template>
  <div>
    <MyDialog title="标题123" ref="myDialog1" width="200px" :beforeClose="beforeClose1">
      <template v-slot:content>
        <p>不能拖拽的dialog</p>
      </template>
      <template v-slot:footer>
        <el-button type="primary" size="default" @click="closeMyDialog1">关闭1</el-button>
        <el-button type="primary" size="default" @click="closeMyDialog1">确认1</el-button>
      </template>
    </MyDialog>
    <MyDialog title="标题456" ref="myDialog2" draggable center width="700px" :beforeClose="beforeClose2">
      <template v-slot:content>
        <p>可以拖拽的dialog</p>
      </template>
      <template v-slot:footer>
        <el-button type="primary" size="default" @click="closeMyDialog2">关闭2</el-button>
        <el-button type="primary" size="default" @click="closeMyDialog2">确认2</el-button>
      </template>
    </MyDialog>
    <el-button type="primary" size="default" @click="openMyDialog1">打开dialog1</el-button>
    <el-button type="primary" size="default" @click="openMyDialog2">打开dialog2</el-button>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import MyDialog from '@/components/myDialog/index.vue'
const myDialog1 = ref(null)
const myDialog2 = ref(null)
const openMyDialog1 = () => {
  myDialog1.value.open()
}
const openMyDialog2 = () => {
  myDialog2.value.open()
}

const closeMyDialog1 = () => {
  myDialog1.value.close()
}
const closeMyDialog2 = () => {
  myDialog2.value.close()
}

const beforeClose1 = done => {
  console.log(done)
}
const beforeClose2 = done => {
  console.log(done)
}
</script>

<style scoped lang="scss"></style>
  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue2 可以通过对 el-dialog 进行二次封装来满足自己的需求。一般来说,我们会在项目封装一些基础组件,比如说对话框、表格、表单等等,这些组件封装可以使得我们在使用时更加方便快捷。 对于 el-dialog二次封装,一般需要注意以下几点: 1. 组件名和引入方式:在封装组件时,需要给组件起一个唯一的名字,避免和其他组件重名。同时,为了方便引入,可以将组件导出为一个单独的 js 文件,然后在需要使用的地方引入。 2. 组件属性:在封装组件时,需要考虑到使用者的需求,将一些常用的属性暴露出来,同时也可以自定义一些属性来更好地满足需求。 3. 插槽:el-dialog 有一些插槽,比如 title、footer 等,我们需要将这些插槽暴露出来,方便使用者进行自定义。 下面是一个简单的 el-dialog 二次封装示例: ``` <template> <el-dialog :title="title" :visible.sync="visible" :width="width" :before-close="beforeClose" :close-on-press-escape="closeOnPressEscape" :lock-scroll="lockScroll" :custom-class="customClass" :close-on-click-modal="closeOnClickModal" :show-close="showClose" > <slot name="header"></slot> <slot></slot> <slot name="footer"></slot> </el-dialog> </template> <script> export default { name: 'MyDialog', props: { title: { type: String, default: '' }, visible: { type: Boolean, default: false }, width: { type: String, default: '50%' }, beforeClose: Function, closeOnPressEscape: { type: Boolean, default: true }, lockScroll: { type: Boolean, default: true }, customClass: { type: String, default: '' }, closeOnClickModal: { type: Boolean, default: true }, showClose: { type: Boolean, default: true } } } </script> ``` 在这个示例,我们将 el-dialog 的一些常用属性暴露出来,同时将 header、content、footer 三个插槽暴露出来,方便使用者自定义。在使用时,只需要引入这个组件,然后像使用 el-dialog 一样使用即可。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

bbamx.

谢谢您

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

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

打赏作者

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

抵扣说明:

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

余额充值