element-plus中ElmessageBox弹窗内容进行vnode操作

前言

在工作中遇到elmessagebox这个组件内容改变遇到的问题。elmessageBox大部分message部分为纯文字,但是也难免像我目前一样需要存入input框类似的功能。我自己琢磨了有一会才写出来,所以做个记录。

目的

我的目的是为了实现类似于一个获取验证码验证的功能。如图:

实现

打开’取消‘按钮为showCancelButton

message识别vnode结构为dangerouslyUseHTMLString

这样就可以使用h(),可以进行识别vnode结构,我们需要引用Elform中时就可以从elementPlus中导入进来。而其中最重要的功能为input标签,如下代码实现:

import { h } from 'vue'
import { ElMessage, ElMessageBox, ElForm, ElFormItem, ElButton } from 'element-plus'
const paramsForm = ref<any>({ code :''})
ElMessageBox({
        title: '验证安全手机',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        showCancelButton: true,
        dangerouslyUseHTMLString: true,
        message: h('div', null, [
            h('div', null, `验证码将发送到手机 ${props.phoneNumber}`),
            h('div', { style: 'color:#ccc;margin-bottom:20px;' }, `如果长时间未收到验证码,请检查是否将运营商拉黑`),
            h(ElForm, { modelValue: paramsForm.value }, [
                h(ElFormItem, { label: '填写验证码:', prop: 'code' }, [
                    h('div', { style: "display:flex" }, [
                        h("input", {
                            value: paramsForm.value.code,
                            id: "hinput",
                            oninput: paramsForm.value.code,
                            placeholder: "验证码六位",
                        }),
                        h(ElButton, { onClick: handleCode, id: 'codeId' }, '获取验证码')
                    ])
                ])
            ])
        ])
    })

其他

我们获取到验证码后将进行倒计时,而这个功能如何实现呢?

如下代码:

const props = defineProps(['phoneNumber'])
const ifhandleCode = ref(false)
const ifqueryCode = ref('')
const timer = ref<any>()
const num = ref(60)

// 获取验证码
const handleCode = async () => {
//获取验证码的接口,传入电话号码
       await sendCode(props.phoneNumber).then((res) => {
           if (res.data.type === "success") {
                // 打开锁
                ifhandleCode.value = true
                const object = JSON.parse(res.data.result)
                // 将验证码写入本地,后面进行对比验证是否输入正确
                ifqueryCode.value = object.obj
                // 进行倒计时
                timers()
            }
        })
}

// 倒计时函数
const timers = () => {
    // 获取到真实dom id为codeId,上面代码有id标识
    const code = document.getElementById('codeId') as any
    // 当num为0时
    if (num.value <= 0) {
        clearTimeout(timer.value)
        ifhandleCode.value = false
        num.value = 60
        code.children[0].innerHTML = '获取验证码'
    } else {
        // 判断锁是否开的情况
        if (ifhandleCode.value) {
            num.value = num.value - 1
            // 倒计时内容
            code.children[0].innerHTML = num.value + 's'
            // 形成闭包
            timer.value = setTimeout(() => {
                timers()
            }, 1000)
        }
    }
}

最后

在ElmessageBox内实现对dom操作就是这样实现的,感谢观看~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Element Plus的组件库,可以通过在ElNotification组件的message属性渲染一个Vue.VNode来实现更复杂的功能。\[2\]在官方文档,给出了两种基本写法来使用message属性。一种是将message属性设置为一个字符串,例如`ElMessage('this is a message.')`。另一种是将message属性设置为一个通过h函数构造的VNode,例如: ```javascript ElMessage({ message: h('p', null, \[ h('span', null, 'Message can be '), h('i', { style: 'color: teal' }, 'VNode'), \]), }) ``` 通过这种方式,可以在ElNotification组件渲染出更加复杂的内容,比如包含HTML标签和样式的消息。 #### 引用[.reference_title] - *1* [从零开始Vue3+Element Plus后台管理系统(十)——自定义水印指令与全局注册](https://blog.csdn.net/immocha/article/details/130681243)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [【Element-plus+Vue3】使用VNode作为ElNotification组件的message属性](https://blog.csdn.net/Catherine_qingzhu/article/details/129928458)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值