Vue3+Element-Plus二次封装的弹框组件

前言: 在上一篇文章中使用到了自己封装的弹框组件,这个组件是上一家公司刚入职的时候写的,觉得用起来挺简单的,这里分享一下

1. 项目初始化(看上一篇文章即可,里面有详细的初始化步骤)

2. 下载项目依赖

这个弹框组件只需要element-plus组件库即可

npm install element-plus
// 或
yarn add element-plus

3. 编写组件

// project/src/components/DialogModal/index.vue
<!--
 * @Author: wangzhiyu <w19165802736@163.com>
 * @version: 1.1.1
 * @Date: 2023-11-22 09:56:16
 * @LastEditTime: 2024-01-18 13:55:56
 * @Descripttion: 弹框组件
-->
<template>
  <slot name="text" :row="dialog">
    <!-- 默认内容 -->
    <el-button type="primary" @click="dialog.visible = true">点击打开弹框</el-button>
  </slot>
  <el-dialog :close-on-press-escape="false" v-model="dialog.visible" :append-to-body="true" :title="title" :before-close="handleClose" :width="width" :close-on-click-modal="false" @close="close" destroy-on-close draggable="true" ref="DialogModel">
    <div :style="{ height: height }">
      <slot name="body" :row="dialog">
        <!-- 默认内容 -->
        弹框内部内容
      </slot>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <slot name="footer" :row="dialog">
          <!-- 默认内容 -->
          <el-button type="primary" @click="dialog.visible = false">确认</el-button>
          <el-button @click="dialog.visible = false">关闭</el-button>
        </slot>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive, ref } from 'vue';

const props = defineProps({
  // 弹框头部标题
  title: {
    type: String,
    required: true,
  },
  // 弹框宽度
  width: {
    type: String,
    default: '70%',
  },
  // 弹框高度
  height: {
    type: String,
    default: '',
  },
});
const emits = defineEmits(['close']);
// 弹框实例
const DialogModel = ref();
const dialog = reactive({
  visible: false,
});
const close = () => {
  emits('close');
};
const handleClose = () => {
  dialog.visible = false;
};
const getDialogRef = () => {
  return DialogModel.value;
};
defineExpose({
  dialog,
  getDialogRef,
});
</script>
<style scoped>
.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>

4. 弹框组件挂载全局

// project/src/components/index.js
/*
 * @Author: wangzhiyu <w19165802736@163.com>
 * @version: 1.1.1
 * @Date: 2023-11-22 09:56:16
 * @LastEditTime: 2023-12-04 13:56:35
 * @Descripttion: 全局components
 */
import DialogModal from './DialogModal/index.vue';

// 全局方法挂载
export function components(app) {
  app.component('DialogModal', DialogModal);
}

// project/src/main.js

// ... 其他代码
import { components } from './components/index';
// 全局组件挂载
components(app);

5. 使用弹框组件

<template>
 // title: 弹框标题
 // ref: 弹框实例
 // @close: 弹框关闭方法
 // draggable: 是否允许拖动弹框
 // 其他API: ......
 <DialogModal title="弹框标题" ref="dialogModalRef" @close="close" draggable="true">
     // 触发打开弹框的按钮
    <template #text="{ row }">
      <!-- <el-button @click="openModal(row)">打开弹框</el-button> -->
      <!-- 这里使用一个空白的span是为了让默认的text插槽的内容不显示,使用ref调用DialogModal组件内部的方法来打开弹框 -->
      <span></span>
    </template>
    
    <!-- 弹框内部插槽 -->
    <template #body>
      <span>body</span>
    </template>
    
    <!-- 弹框底部插槽 -->
    <template #footer="{row}">
       <el-button>取消</el-button>
       // 关闭弹框
       <el-button @click="()=>row.visible=false">确定</el-button>
    </template>
  </DialogModal>
 </template>
 <script setup>
 // 弹框实例
 const dialogModalRef = ref();
 
 // 补充:
 // dialogModalRef.value.dialog.visible 属性可以控制弹框的显示隐藏
 // 弹框组件内部的所有抛出的API,dialogModalRef都可以调用
 </script>

6. 效果图:

image.png

7. 总结: 以上就是使用Vue3二次封装的Element-Plus,这个组件的自定义性非常强,但是同样的,使用起来比较麻烦,用法也比较多,有兴趣的朋友可以自己挖掘一下哦😋

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Vue2中,对于element-ui组件的二次封装,可以按照以下步骤进行: 1. 需求分析:明确需要封装的element-ui组件,以及需要添加的功能和配置项。 2. 创建父组件:编写父组件的template和script代码,其中template中调用封装组件,script中定义需要传递给封装组件的props属性。 3. 创建封装组件:编写封装组件的template和script代码。在template中使用element-ui组件,并根据需要进行样式和布局的调整。在script中定义props属性,接收父组件传递的值,并监听element-ui组件的事件,触发update事件给父组件。 4. 通过临时变量传递值:由于父组件传递给封装组件的props不能直接作为封装组件的v-model属性传递给element-ui组件,所以需要在封装组件中定义一个临时变量来存储值,并将该变量与element-ui组件进行绑定。 5. 完成打通:在封装组件中监听中间件,接收到element-ui组件的update事件后,再将该事件传递给父组件。 总结来说,Vue2中对于element-ui组件的二次封装,需要创建父组件封装组件,通过props属性传递值,并在封装组件中监听element-ui组件的事件并触发update事件给父组件。同时,需要使用临时变量来传递值给element-ui组件。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Vue3+ts+element-plus 组件的二次封装-- 页脚分页el-pagination的二次封装](https://blog.csdn.net/cs492934056/article/details/128096257)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值