element-plus使用h和render函数,实现Service弹出Dialog

简介: element-plus使用h和render函数,实现Service弹出Dialog

在element-plus中,Messagebox和Message都实现了全局方法。但是Dialog就没有实现。

本着自己动手丰衣足食的原则。自己动手写一个。

定义一个ModalService.ts文件

import { h, render } from 'vue'
class ModalService {
    /**
     * 创建一个弹出框
     * @param component 组件
     * @param opt 配置
     */
    static Create(component: any, props: any, opt?: ModalOption,) {
        return new Promise((resolve, reject) => {
            const close = () => {
                render(null, container)
                document.body.removeChild(container);
                resolve(true)
            }
            const modelValue = true
            const dialogProps = {
                modelValue: modelValue,
                title: opt?.title,
                draggable: opt?.draggable,
                destroyOnClose: true,
                onClosed: close,
            }
            const closeHandler = () => {
                if (vNode.component?.props.modelValue) {
                    vNode.component!.props.modelValue = false
                }
            }
            const container = document.createElement('div')
            document.body.appendChild(container)
            var vNode = h(ElDialog, dialogProps, {
                default: () => {
                    let type = typeof component;
                    if (type == 'string' || type == 'number') {
                        return h('div', component)
                    } else {
                        return h(component, {
                            ...props,
                            onClose: closeHandler
                        })
                    }
                },
                header: () => {
                    if (dialogProps) {
                        let type = typeof dialogProps.title
                        if (type == 'string' || type == 'number') {
                            return h('span', dialogProps.title)
                        }
                        return h(dialogProps.title, null)
                    }
                }
            });
            render(vNode, container)
            return vNode.component;
        })
    }
}

使用

import HelloWorld from "../../../components/HelloWorld.vue"
const test = () => {
    ModalService.Create(HelloWorld, { msg: '传递参数' }, { title: '卧槽' }).then(() => {
        console.log('成功关闭')
    });
}

当然这只是一个demo,很多el-dialog组件的参数和事件没有考虑到。以后有空再完善了。

目录
相关文章
Vue3 子/父组件相互调用
Vue3 子/父组件相互调用
465 0
|
JSON 数据格式
解决报错TypeError: Converting circular structure to JSON --> starting at object with constructor
解决报错TypeError: Converting circular structure to JSON --> starting at object with constructor
|
8月前
|
机器学习/深度学习 人工智能 自动驾驶
企业内训|模拟AI场景课程——某汽车厂商
4月18日和19日,东北某市,TsingtaoAI团队为某汽车厂商的智能驾驶业务和研发团队交付“模拟AI场景课程”。本课程基于该厂商在AI领域的战略布局,结合汽车行业智能化转型趋势,以“场景化、实战化、前瞻性”为核心,聚焦AI技术从理论到落地的全链路。通过模拟真实业务场景(如智能座舱优化、智能制造、自动驾驶仿真),帮助学员掌握AI基础能力,并快速应用于研发、生产、营销等环节。
294 4
|
API
Pinia 实用教程【Vue3 状态管理】状态持久化 pinia-plugin-persistedstate,异步Action,storeToRefs(),修改State的 $patch,$reset
Pinia 实用教程【Vue3 状态管理】状态持久化 pinia-plugin-persistedstate,异步Action,storeToRefs(),修改State的 $patch,$reset
3747 1
|
缓存 网络协议 Linux
通过实验深入了解 TCP 连接的建立和关闭
TCP/IP 这个主题很多文章比较陈旧,且以讹传讹的东西太多,所以本文作者结合了理论和实践去写,旨在通过一系列实验帮助读者深入理解 TCP 连接的建立过程。
449 13
在Vue3项目中使用 vue3-seamless-scroll 无缝滚动插件
本文介绍了如何在Vue3项目中使用`vue3-seamless-scroll`插件实现无缝滚动效果,并提供了详细的示例代码和运行效果。
6227 0
|
开发者 Windows Android开发
跨平台开发新选择:揭秘Uno Platform与.NET MAUI优劣对比,帮你找到最适合的框架,告别选择困难症!
【8月更文挑战第31天】本文对比了备受关注的跨平台开发框架Uno Platform与.NET MAUI的特点、优势及适用场景。Uno Platform基于WebAssembly和WebGL技术,支持Windows、iOS、Android及Web平台,而.NET MAUI由微软推出,旨在统一多种UI框架,支持Windows、iOS和Android。两者均采用C#和XAML进行开发,但在性能、平台支持及社区生态方面存在差异。Uno Platform在Web应用方面表现出色,但性能略逊于原生应用;.NET MAUI则接近原生性能,但不支持Web平台。开发者应根据具体需求选择合适的框架。
623 0
|
编解码 JavaScript 前端开发
python如何解决js逆向混淆?
python如何解决js逆向混淆?
503 0
【UI】 修改element-ui input输入框placeholder提示信息、占位符的样式
【UI】 修改element-ui input输入框placeholder提示信息、占位符的样式
1566 0