vue3+ts+elementui-plus二次封装弹框

一、弹框组件BaseDialog

复制代码
<template>
      <div class='main'>
            <el-dialog v-model="visible" :title="title" :width="dialogWidth" :before-close="handleClose">
                  <!-- 内容插槽 -->
                  <slot></slot>
                  <template #footer>
                        <span class="dialog-footer">
                              <el-button v-if="showCancelButton" @click="handleClose">取消</el-button>
                              <el-button v-if="showConfirmButton" type="primary" @click="handleConfirm">
                                    确认
                              </el-button>
                        </span>
                  </template>
            </el-dialog>
      </div>
</template>

<script lang='ts' setup>
import { ElMessageBox } from 'element-plus'
import { ref, reactive, getCurrentInstance, onMounted, defineExpose, defineEmits } from 'vue'
/**
 * 传入的props变量
*/
const props = defineProps({
      title: {
            type: String,
            default: '提示',
      },
      dialogWidth: {
            type: String,
            default: '40%',
      },
      showCancelButton: {
            type: Boolean,
            default: true,
      },
      showConfirmButton: {
            type: Boolean,
            default: true,
      },

})
/**
 * 发射给父组件的方法 
 * 用于子组件给父组件传值或调用父组件方法
*/
const emits = defineEmits(['submit', 'close'])
const visible = ref(false)
// 关闭弹框
const handleClose = () => {
      emits('close')
}
// 打开弹框
const handleOpen = () => {
      visible.value = true
}
// 确认事件
const handleConfirm = () => {
      emits('submit')
}
/**
 * 暴露给父组件的方法
 * 用于父组件调用子组件方法或获取子组件属性值
*/
defineExpose({ handleOpen, handleClose, visible })
onMounted(() => {
})

</script>
<style scoped lang='scss'>
</style>

二、在index.vue中使用

复制代码
<el-button @click="showDialog">点击出现弹框</el-button>
		<BaseDialog ref="baseDialog" @submit="handleSubmit" @close="handleClose">
			<div>
				<el-input placeholder="Please input" />
			</div>
		</BaseDialog>

<script lang='ts' setup>
import BaseDialog from '@/components/BaseDialog/index.vue'
import { ref, reactive, getCurrentInstance, onMounted } from 'vue'
onMounted(() => {
})
// 获取子组件的ref
let baseDialog = ref()
// 点击出现弹框
const showDialog = () => {
	// 调用子组件方法,打开弹框
	baseDialog.value.handleOpen()
}
// 弹框确认事件
const handleSubmit = () => {
	console.log('我是父组件中的确认事件')
}
// 弹框取消事件 
const handleClose = () => {
	baseDialog.value.visible = false
}
</script>

三、效果

相关推荐
爱吃的强哥几秒前
vue3 使用 vite 管理多个项目,实现各子项目独立运行,独立打包
前端·javascript·vue.js
涵信9 分钟前
第十节:性能优化高频题-虚拟DOM与Diff算法优化
javascript·vue.js·性能优化
拖孩16 分钟前
【Nova UI】十一、组件库中 Icon 组件的测试、使用与全局注册全攻略
前端·javascript·vue.js·ui·sass
天天扭码1 小时前
深入解析 JavaScript 中的每一类函数:从语法到对比,全面掌握适用场景
前端·javascript·面试
凉豆菌2 小时前
在html中如何创建vue自定义组件(以自定义文件上传组件为例,vue2+elementUI)
vue.js·elementui·html
广西千灵通网络科技有限公司2 小时前
基于 springboot+vue+elementui 的办公自动化系统设计(
vue.js·spring boot·elementui
北上ing2 小时前
同一页面下动态加载内容的两种方式:AJAX与iframe
前端·javascript·ajax
纪元A梦2 小时前
华为OD机试真题——推荐多样性(2025A卷:200分)Java/python/JavaScript/C++/C语言/GO六种最佳实现
java·javascript·c++·python·华为od·go·华为od机试题
小墨宝3 小时前
js 生成pdf 并上传文件
前端·javascript·pdf
www_pp_4 小时前
# 构建词汇表:自然语言处理中的关键步骤
前端·javascript·自然语言处理·easyui