满口金牙 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 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改