当前位置: 首页 > news >正文

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>

三、效果

 

http://www.lryc.cn/news/102436.html

相关文章:

  • ffmpeg批量分割视频解决视频前几秒黑屏的问题解决
  • nodejs + express 调用本地 python程序
  • 微信小程序代码优化3个小技巧
  • 某行动态cookie反爬虫分析
  • 恒运资本:A股、港股全线爆发,沪指突破3300点,恒指重返2万点上方
  • Rust vs Go:常用语法对比(十二)
  • jmeter接口测试、压力测试简单实现
  • PysparkNote006---pycharm加载spark环境
  • 19套项目实战系列--Spring Cloud Spring Boot(整套源码)
  • TCP/IP协议详解(二)
  • Linux6.2 ansible 自动化运维工具(机器管理工具)
  • 前端面试题 —— React (二)
  • 【分享帖】LCD的MCU接口和SPI接口详解
  • 【Java】使用@Expose注解和excludeFieldsWithoutExposeAnnotatGson()方法将toJson()过程的部分字段忽略
  • 移动硬盘不显示怎么办?正确解决方式看这里!
  • MySQL 5.7.39 关于时间精度
  • 宝塔设置云服务器mysql端口转发,实现本地电脑访问云mysql
  • centos下安装ftp-读取目录列表失败-
  • 0101sub-process /usr/bin/dpkg returned an error code-dpkg-linux问题集
  • 流控平台Sentinel搭建和接入教程
  • 使用 docker 一键部署 MongoDB
  • 【深度学习】Inst-Inpaint: Instructing to Remove Objects with Diffusion Models,指令式图像修复
  • 创建维基WIKI百科和建立百度百科有何不同?
  • Python小红书旋转验证码识别
  • ELK搭建
  • webyog最新社区版免费版下载地址
  • [SQL挖掘机] - 窗口函数 - dense_rank
  • stable diffusion
  • web3行业有哪些职业发展路径?
  • MATLAB算法实战应用案例精讲-【自动驾驶】相控阵天线方向图