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组件的参数和事件没有考虑到。以后有空再完善了。

目录
相关文章
|
缓存
IDEA 卡住不动的解决办法,超级管用。。。
IDEA 卡住不动的解决办法,超级管用。。。
3805 0
IDEA 卡住不动的解决办法,超级管用。。。
Vue3 子/父组件相互调用
Vue3 子/父组件相互调用
628 0
|
JSON 数据格式
解决报错TypeError: Converting circular structure to JSON --> starting at object with constructor
解决报错TypeError: Converting circular structure to JSON --> starting at object with constructor
|
10月前
|
机器学习/深度学习 人工智能 自动驾驶
企业内训|模拟AI场景课程——某汽车厂商
4月18日和19日,东北某市,TsingtaoAI团队为某汽车厂商的智能驾驶业务和研发团队交付“模拟AI场景课程”。本课程基于该厂商在AI领域的战略布局,结合汽车行业智能化转型趋势,以“场景化、实战化、前瞻性”为核心,聚焦AI技术从理论到落地的全链路。通过模拟真实业务场景(如智能座舱优化、智能制造、自动驾驶仿真),帮助学员掌握AI基础能力,并快速应用于研发、生产、营销等环节。
415 4
|
缓存 网络协议 Linux
通过实验深入了解 TCP 连接的建立和关闭
TCP/IP 这个主题很多文章比较陈旧,且以讹传讹的东西太多,所以本文作者结合了理论和实践去写,旨在通过一系列实验帮助读者深入理解 TCP 连接的建立过程。
563 13
|
API
Pinia 实用教程【Vue3 状态管理】状态持久化 pinia-plugin-persistedstate,异步Action,storeToRefs(),修改State的 $patch,$reset
Pinia 实用教程【Vue3 状态管理】状态持久化 pinia-plugin-persistedstate,异步Action,storeToRefs(),修改State的 $patch,$reset
4124 1
|
JSON JavaScript 定位技术
Echarts 绘制地图(中国、省市、区县),保姆级教程!
Echarts 绘制地图(中国、省市、区县),保姆级教程!
|
JavaScript
vue组件封装 | 数字输入框(限制只能输入数字的input,可以指定小数点位数,最大值、最小值)
vue组件封装 | 数字输入框(限制只能输入数字的input,可以指定小数点位数,最大值、最小值)
676 7
在Vue3项目中使用 vue3-seamless-scroll 无缝滚动插件
本文介绍了如何在Vue3项目中使用`vue3-seamless-scroll`插件实现无缝滚动效果,并提供了详细的示例代码和运行效果。
7531 0