vue3封装命令式弹窗组件

2024-08-30 20:28

本文主要是介绍vue3封装命令式弹窗组件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、前言

相信大家都封装过弹窗组件,基本思路都是父组件给子组件传递一个变量,子组件props进行接收,当点击确认或者关闭按钮时,通过emit回传事件供父组件调用。这种封装方式缺点是复用性查、使用频繁时需要定义多份{isVisible、handleSubmit、handleClose}代码,代码冗余,今天分享一种命令式组件封装的方式。

二、什么是命令式组件

命令式组件封装是一种将功能封装在组件内部,并通过命令式的方式进行调用和控制的封装方法。在命令式组件封装中,组件负责封装一定的功能逻辑,并提供一组接口或方法,供外部代码调用来触发和控制组件的行为。调用方式大致为:

async function showMsg(){const [,res]= await msgBox('测试内容',{type:'success',iconType:'question'})if(res){consloe.log('点击了确定')}else{console.log('点击了取消')}
}

三、开始封装

首先创建一个MessageBox.vue文件,编写组件样式代码,这里我使用的是unocss进行编写。

<script lang="ts" setup>
import { computed, onMounted, ref } from "vue"
import { ElButton, ElDialog } from 'element-plus'
export interface HuiMsgBoxProp {/** 控制图标展示类型 info:叹号 success:钩 question:问号 */iconType:'info' | 'success' | 'question',/** 控制图标展示的颜色 */type:'info' | 'warning' | 'success' | 'danger',/** 弹窗显示的内容 */content:string,/** 取消按钮的文本 */cancelText:string,/** 确定按钮的文本 */confirmText:string,/** 关闭事件 */closeBox: ()=> void,/** 确定事件事件 */confirmHandler:()=> void,/** 取消事件 */cancelHandler:()=> void,
}
const { iconType, type, content, cancelText, confirmText, closeBox, confirmHandler, cancelHandler } = withDefaults(defineProps<HuiMsgBoxProp>(), {iconType: 'info',type: 'info',cancelText: '取消',confirmText: '确定',
})const iconTypeClass = computed<string>(() => {const iconTypeClassList = {info: 'text-disabled',warning: 'text-warning',success: 'text-success',danger: 'text-danger',}return iconTypeClassList[type]
})const iconColorClass = computed<string>(() => {const iconColorClassList = {info: 'i-com-gantanhao',success: 'i-com-gou1',question: 'i-com-wenhao',}return iconColorClassList[iconType]
})// 控制显示处理
const isVisible = ref(false)
/*** 组件展示*/
const show = () => {isVisible.value = true
}/*** 处理动画 (render 函数的渲染,会直接进行)*/
onMounted(() => {show()
})/*** 取消事件*/
const onCancelClick = () => {if (cancelHandler) {cancelHandler()}close()
}/*** 确定事件*/
const onConfirmClick = () => {if (confirmHandler) {confirmHandler()}closeBox()
}// 关闭动画处理时间
const duration = '0.5s'
/*** 关闭事件,保留动画执行时长*/
const close = () => {isVisible.value = false// 延迟一段时间进行关闭setTimeout(() => {if (closeBox) {closeBox()}}, parseInt(duration.replace('0.', '').replace('s', '')) * 100)
}
</script><template><div class="hua5-message-box"><ElDialogv-model="isVisible"width="400"@closed="close"><div class="flex justify-center flex-center h-110"><div><i :class="[iconTypeClass,iconColorClass,'icon-com !text-27']" /></div><div class="text-14 font-bold text-normal ml-11">{{ content }}</div></div><template #footer><div class="dialog-footer"><ElButton class="!text-primary !border !border-1 !border-primary" @click="onCancelClick">{{ cancelText }}</ElButton><ElButton type="primary" class="w-100px" @click="onConfirmClick">{{ confirmText }}</ElButton></div></template></ElDialog></div>
</template><style lang="scss">
.hua5-message-box{.el-dialog{border-radius: 8px !important;}.el-dialog .el-dialog__header{background-color: #fff !important;}.el-dialog .el-dialog__footer{background-color: #fff !important;}
}
</style>

组件需要接收参数

export interface HuiMsgBoxProp {/** 控制图标展示类型 info:叹号 success:钩 question:问号 */iconType:'info' | 'success' | 'question',/** 控制图标展示的颜色 */type:'info' | 'warning' | 'success' | 'danger',/** 弹窗显示的内容 */content:string,/** 取消按钮的文本 */cancelText:string,/** 确定按钮的文本 */confirmText:string,/** 关闭事件 */closeBox: ()=> void,/** 确定事件事件 */confirmHandler:()=> void,/** 取消事件 */cancelHandler:()=> void,
}

其次创建index.ts文件,由于组件调用方式是通过一个函数进行调用的,并提供.then.catch方法,所以需要编写一个函数,该函数返回一个Promise。当调用该函数,创建组件实例,组件进行挂载。

import { h, render } from 'vue'
import confirmComponent from './message-box.vue'
import { to } from "@hua5/hua5-utils"export interface PayLoadType {/** 控制图标展示类型 info:叹号 success:钩 question:问号 */iconType?:'info' | 'success' | 'question',/** 控制图标展示的颜色 */type?: "info" | "success" | "danger" | "warning",/** 取消按钮的文本 */cancelText?:string,/** 确定按钮的文本 */confirmText?:string
}
export const hua5MsgBox = (content: string, payLoad:PayLoadType = {}) => {const { iconType = 'info', type = 'info', cancelText, confirmText } = payLoadreturn new Promise((resolve) => {// 取消按钮事件const cancelHandler = () => {resolve(false)}// 确定按钮事件const confirmHandler = () => {resolve(true)}// 关闭弹层事件const closeBox = () => {render(null, document.body)}// 1. 生成 vnodeconst vnode = h(confirmComponent, {content,iconType,type,cancelText,confirmText,cancelHandler,confirmHandler,closeBox,})// 2. render 渲染render(vnode, document.body)})
}export const msgBox = (content: string, payLoad?:PayLoadType) => {return to(hua5MsgBox(content, payLoad))
}

四、to函数

/*** @param { Promise } promise* @param { Object= } errorExt - Additional Information you can pass to the err object* @return { Promise }*/
export function to(promise: Promise<any>): Promise<any> {return promise.then(res => [null, res]).catch(error => [error, null])
}

这篇关于vue3封装命令式弹窗组件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1121845

相关文章

Golang如何对cron进行二次封装实现指定时间执行定时任务

《Golang如何对cron进行二次封装实现指定时间执行定时任务》:本文主要介绍Golang如何对cron进行二次封装实现指定时间执行定时任务问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录背景cron库下载代码示例【1】结构体定义【2】定时任务开启【3】使用示例【4】控制台输出总结背景

前端如何通过nginx访问本地端口

《前端如何通过nginx访问本地端口》:本文主要介绍前端如何通过nginx访问本地端口的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、nginx安装1、下载(1)下载地址(2)系统选择(3)版本选择2、安装部署(1)解压(2)配置文件修改(3)启动(4)

HTML中meta标签的常见使用案例(示例详解)

《HTML中meta标签的常见使用案例(示例详解)》HTMLmeta标签用于提供文档元数据,涵盖字符编码、SEO优化、社交媒体集成、移动设备适配、浏览器控制及安全隐私设置,优化页面显示与搜索引擎索引... 目录html中meta标签的常见使用案例一、基础功能二、搜索引擎优化(seo)三、社交媒体集成四、移动

HTML input 标签示例详解

《HTMLinput标签示例详解》input标签主要用于接收用户的输入,随type属性值的不同,变换其具体功能,本文通过实例图文并茂的形式给大家介绍HTMLinput标签,感兴趣的朋友一... 目录通用属性输入框单行文本输入框 text密码输入框 password数字输入框 number电子邮件输入编程框

HTML img标签和超链接标签详细介绍

《HTMLimg标签和超链接标签详细介绍》:本文主要介绍了HTML中img标签的使用,包括src属性(指定图片路径)、相对/绝对路径区别、alt替代文本、title提示、宽高控制及边框设置等,详细内容请阅读本文,希望能对你有所帮助... 目录img 标签src 属性alt 属性title 属性width/h

CSS3打造的现代交互式登录界面详细实现过程

《CSS3打造的现代交互式登录界面详细实现过程》本文介绍CSS3和jQuery在登录界面设计中的应用,涵盖动画、选择器、自定义字体及盒模型技术,提升界面美观与交互性,同时优化性能和可访问性,感兴趣的朋... 目录1. css3用户登录界面设计概述1.1 用户界面设计的重要性1.2 CSS3的新特性与优势1.

Python中对FFmpeg封装开发库FFmpy详解

《Python中对FFmpeg封装开发库FFmpy详解》:本文主要介绍Python中对FFmpeg封装开发库FFmpy,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、FFmpy简介与安装1.1 FFmpy概述1.2 安装方法二、FFmpy核心类与方法2.1 FF

HTML5 中的<button>标签用法和特征

《HTML5中的<button>标签用法和特征》在HTML5中,button标签用于定义一个可点击的按钮,它是创建交互式网页的重要元素之一,本文将深入解析HTML5中的button标签,详细介绍其属... 目录引言<button> 标签的基本用法<button> 标签的属性typevaluedisabled

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,