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

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

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);
} 

// 使用

	import {SingPop} from './singPop.jsx'
	// 可通过一个按钮来触发
	SingPop('哈哈哈',{
					cancel:()=>{
						console.log('cancel');
					},
					agree:()=>{
						console.log('agree')
					}
				})
  • 11
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值