满口金牙 2021-12-09 13:19 采纳率: 91.5%
浏览 167
已结题

vue3 element-plus 谁会改造ElMessageBox 组件

目的: 封装一个 myMessage 函数,可以传数组和字符串内容进去

import { ElMessageBox, ElMessage } from 'element-plus'
// 目的: 改造封装一个 myMessage 函数,可以传数组和字符串内容进去
export function myMessage2(str,title) {
  var newArr
  const h = this.$createElement; // 这里写错了
  if (Array.isArray(str)){ // 如果str传进来是数组,就遍历
    newArr =[]
    for(let i =0; i < str.length; i++){
      newArr.push(h('h2', null, str[i]))
    }
  }else{
    newArr = h('span', null , str)
  }
  ElMessageBox({
    title: title,
    message: h('h1', null, newArr),
    showCancelButton: false,
    confirmButtonText: 'OK',
  })
}


请给有效的代码
https://element-plus.gitee.io/zh-CN/component/message-box.html#%E4%B8%AA%E6%80%A7%E5%8C%96%E8%AE%BE%E7%BD%AE%E4%BD%A0%E7%9A%84%E5%BC%B9%E6%A1%86

  • 写回答

1条回答 默认 最新

  • mrjimin 2021-12-09 13:43
    关注
    
            import {  h } from 'vue'
            import { ElMessageBox, ElMessage } from 'element-plus'
            // 目的: 改造封装一个 myMessage 函数,可以传数组和字符串内容进去
            export function myMessage2(str, title) {
                var newArr
                // const h = this.$createElement; // 这里写错了
                if (Array.isArray(str)) { // 如果str传进来是数组,就遍历
                    newArr = []
                    for (let i = 0; i < str.length; i++) {
                        newArr.push(h('h2', null, str[i]))
                    }
                } else {
                    newArr = h('span', null, str)
                }
                ElMessageBox({
                    title: title,
                    message: h('h1', null, newArr),
                    showCancelButton: false,
                    confirmButtonText: 'OK',
                })
            }
    
    

    从vue中引入h函数

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月17日
  • 已采纳回答 12月9日
  • 修改了问题 12月9日
  • 创建了问题 12月9日

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)