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>

三、效果

相关推荐
yvvvy11 分钟前
前端性能优化全家桶:从重绘重排到面试连招,一篇搞懂
前端·javascript·面试
小蒜学长24 分钟前
vue家教预约平台设计与实现(代码+数据库+LW)
java·数据库·vue.js·spring boot·后端
串串狗xk28 分钟前
使用 webgl 写的新概念笔记应用《赛博城寨》,在三维开放世界里写笔记
javascript·webgl
页面仔Dony1 小时前
流式数据获取与展示
前端·javascript
前端进阶者2 小时前
electron-vite_20外部依赖包上线后如何更新
前端·javascript·electron
阿虎儿2 小时前
TypeScript 内置工具类型完全指南
前端·javascript·typescript
chxii3 小时前
6.3Element UI 的表单
javascript·vue.js·elementui
张努力3 小时前
从零开始的开发一个vite插件:一个程序员的"意外"之旅 🚀
前端·vue.js
深兰科技3 小时前
深兰科技:搬迁公告,我们搬家了
javascript·人工智能·python·科技·typescript·laravel·深兰科技
chxii3 小时前
6.4 Element UI 中的 <el-table> 表格组件
vue.js·ui·elementui