VUE消息弹窗不重复弹出,只弹出一次

消息弹窗不重复弹出,只弹出一次

文章借鉴:让ElementUI Message消息提示每次只弹出一次

1、在utils里新建一个message.js 文件

/**
 * 重置message,防止重复点击重复弹出message弹框
 * 方法一用法:
 * main.js挂载:Vue.prototype.$messageSingle = message.success;
 *    ———— this.$messageSingle.info("操作失败"); 或者 this.$messageSingle.info({message:‘操作失败‘})
 *    ———— this.$messageSingle.infoTime("操作失败", 1000);
 * */
import { Message } from ‘element-ui‘;
const showMessage = Symbol(‘showMessage‘);
/** 方法一 */
class DoneMessage {
  [showMessage](type, options, single) {
    if (single) {
      if (document.getElementsByClassName(‘el-message‘).length === 0) {
        Message[type](options)
      }
    } else {
      Message[type](options)
    }
  }
  info(options, single = true) {
    this[showMessage](‘info‘, options,single)
  }
  infoTime(options, time, single = true) {
    this[showMessage](‘info‘, { showClose: true, message: options, duration: time == undefined ? 2000 : time  }, single)
  }
  warning(options, single = true) {
    this[showMessage](‘warning‘, options, single)
  }
  warningTime(options, time, single = true) {
    this[showMessage](‘warning‘, { showClose: true, message: options, duration: time == undefined ? 2000 : time }, single)
  }
  error(options, single = true) {
    this[showMessage](‘error‘, options, single)
  }
  errorTime(options, time, single = true) {
    this[showMessage](‘error‘, { showClose: true, message: options, duration: time == undefined ? 2000 : time }, single)
  }
  success(options, single = true) {
    this[showMessage](‘success‘, options, single)
  }
  successTime(options, time, single = true) {
    /*用法: this.$messageSingle.successTime("操作成功", 1000);*/
    console.log(time)
    this[showMessage](‘success‘, { showClose: true, message: options, duration: time == undefined ? 2000 : time }, single)
  }
}

export const message = new DoneMessage();

/** 方法二
 * 用法:
 *    挂载main.js
 *    Vue.prototype.successTip = successTip; ....
 *    this.successTip("操作成功", 2000)
 */
/**
 * 单独使用
 * @param type
 * @param options
 * @param single
 */
function messageCp(type, options, single) {
  if (single) {
    if (document.getElementsByClassName(‘el-message‘).length === 0) {
      Message[type](options)
    }
  } else {
    Message[type](options)
  }
}

export function successTip(msg, time, single = true) {
  const options = { showClose: true, message: msg, duration: time == undefined ? 2000 : time }
  messageCp("success", options, single)
}
export function errorTip(msg, time, single = true) {
  const options = { showClose: true, message: msg, duration: time == undefined ? 2000 : time }
  messageCp("error", options, single)
}
export function infoTip(msg, time, single = true) {
  const options = { showClose: true, message: msg, duration: time == undefined ? 2000 : time }
  messageCp("info", options, single)
}
export function warningTip(msg, time, single = true) {
  const options = { showClose: true, message: msg, duration: time == undefined ? 2000 : time }
  messageCp("warning", options, single)
}

2、在main.js里面引入

//自定义消息弹窗只出现一次
import { message } from ‘@/utils/message‘;

//挂载,弹窗不重复弹出
Vue.prototype.$messageSingle = message;
//可动态设置弹窗关闭时间
Vue.prototype.successTip = successTip;
Vue.prototype.warningTip = warningTip;
Vue.prototype.errorTip = errorTip;

注意:挂载的话只能在vue.use(ElementUI)后面

3、用法

  • 用法:

  • main.js挂载:Vue.prototype.$messageSingle = message;

  • ———— this.$messageSingle.info("操作失败");

  • 或者 this.$messageSingle.info({message:‘操作失败‘});

  • 动态设置弹窗关闭时间

  • ———— this.$messageSingle.infoTime("操作失败", 1000);

VUE消息弹窗不重复弹出,只弹出一次

上一篇:系统集成项目管理工程师10大管理47个过程域输入输出工具(项目成本管理)


下一篇:ssl证书含有的信息包括哪些