Vue3使用jsx和render函数动态插入组件和元素

定义弹框组件(只实现功能,不管样式)

javascript 复制代码
import {render} from 'vue'
export function SingPop(content,handler) {
    const div = document.createElement('div');
    let pop = <div>
        <div>{content}</div>
        <div>
            <button onClick={()=>{
                document.body.removeChild(div);
                console.log('不同意');
                handler.cancel && handler.cancel()

            }}>不同意</button>
           <button onClick={()=>{
                document.body.removeChild(div);
                console.log('同意');
                handler.agree && handler.agree();
            }}>同意</button>
        </div>
    </div>
 
/**
 * render ------ 渲染虚拟 DOM
 * @param 参数1 要被渲染的虚拟 DOM,必选
 * @param 参数2 要渲染的位置,必选
 * @description 虚拟 DOM 创建完成后,需要使用 render 函数,才能在页面中渲染
**/
 render(pop,div);
 document.body.appendChild(div);
} 

// 使用

javascript 复制代码
	import {SingPop} from './singPop.jsx'
	// 可通过一个按钮来触发
	SingPop('哈哈哈',{
					cancel:()=>{
						console.log('cancel');
					},
					agree:()=>{
						console.log('agree')
					}
				})
相关推荐
东方小月6 小时前
从零开发一个 Coding Agent(四):使用状态机校验大模型事件流
前端·人工智能·后端
想做后端的前端7 小时前
WebGL实现FPS游戏
vue.js·游戏·webgl
Csvn7 小时前
🧩 ESM vs CJS 混用的 7 个「天坑」——从 TypeScript 编译到 Node 与浏览器
前端
Csvn7 小时前
🎯 Web 性能 API 集合:Performance Observer 的 5 个冷门妙用
前端
Csvn7 小时前
深入 React 闭包陷阱:从根源上理解并根治 stale closure
前端
whyfail7 小时前
前端学 Spring Boot(8):接口为什么越用越慢?
前端·spring boot·后端
用户059540174468 小时前
LangChain 记忆测试踩坑实录:这两个坑让我排查了 4 小时
前端·css
程序员黑豆8 小时前
鸿蒙应用开发:@Monitor 装饰器使用教程
前端·harmonyos
用户938515635078 小时前
从零构建《天龙八部》知识库:EPUB 加载→文本分割→向量嵌入→Milvus 存储→RAG 问答,一条链路打通
javascript·人工智能·全栈
SamChan909 小时前
在Web应用中集成PDF多语言翻译功能:PDFTranslator API实战指南
前端·python·ai·pdf·yapi·机器翻译