重写element Message 错误提示;错误提示增加复制功能;

重写element  Message 错误提示; 支持notice组件; 全局引入修改;


import errorNotifyCopy from '@/util/errorNotifyCopy.js'
Vue.prototype.$errorNotifyCopy = errorNotifyCopy

/*
* * 注入一个vue的扩展,支持notice组件error时候的流程ID注入 */ let _Vue let installFlag = false const install = (Vue) => { if (Vue === _Vue) return if (installFlag) return _Vue = Vue Vue.mixin({ created () { if (!installFlag) { installFlag = true if (!(this.$Notice && this.$Notice.error) && !(this.$Message && this.$Message.error)) { return } // const fun = this.$Notice.error || this.$Message.error || this.$notify.error this.$Message.error = this.$notify.error = this.$Notice.error = (opt, onClose) => { const {title, message, desc, content, duration = 5000} = {...opt} let str = message || desc || title || content let _title = '' let validateFormTips = ['还有必填项未填', '请上传附件', '内容最多只能输入2000字'] // 过滤掉部分表单校验的提示 let isShowNo = true if (title && desc || title && message || title && content) { str = desc || message || content _title = title } if (desc instanceof Error || message instanceof Error) { str = desc.message || message.message } if (opt.constructor === String) { str = opt } if (validateFormTips.includes(str)) { isShowNo = false } this.$errorNotifyCopy({ message: str, duration: duration, title: _title, isShowInstanceNo: isShowNo }, onClose) // return fun.call(this, ...opt) } } } }) } Vue.use(install)

 

import { Notification } from 'element-ui'
import store from '@/store/modules/common'

let errorNotifyCopy = (options, onClose) => {
  let { message = '', duration = 5000, title = '错误消息', type = 'error', isShowInstanceNo = true } = {...options}
  let No = isShowInstanceNo && store.state.context && store.state.context.Data && store.state.context.Data.InstanceContext.InstanceNo ? '流程号:' + store.state.context.Data.InstanceContext.InstanceNo + ';' : ''
  message = `${No}${message}`
  let htmlMsg = `<span class="err_msg_info"><span class="copyBtn">已复制</span>${message}</span>`
  Notification({
    title: title,
    type: type,
    dangerouslyUseHTMLString: true,
    message: htmlMsg,
    duration: duration,
    customClass: 'err_msg_copy_box',
    onClick: () => {
      event.preventDefault()
      window.event ? window.event.cancelBubble = true : event.stopPropagation()
      let text = message.replace(',', '')
      var textArea = document.createElement('textarea')
      textArea.style.zIndex = '-9999'
      textArea.value = text
      document.body.appendChild(textArea)
      textArea.select()
      document.execCommand('Copy', true)
      document.body.removeChild(textArea)
      event.target.children[0].className = 'copyBtn show'
      setTimeout(function (event) {
        event.target.children[0].className = 'copyBtn'
      }, 800, event)
    },
    onClose: onClose
  })
}

export default errorNotifyCopy

 

posted @ 2022-09-29 18:47  小贝馨  阅读(30)  评论(0编辑  收藏  举报