当前位置: 首页 > article >正文

elementUI提示、通知、弹框每次只出现一个

一、封装辅助函数

/**
 * 防止重复弹出message、Notification、MessageBox弹框
 */

import { Message, Notification, MessageBox } from 'element-ui';

let currentMessage = null;
let currentMessageBox = null;
let currentNotification = null;

/*
 * 防止重复弹出Message
 * @param {Object} config - MessageBox默认的配置参数
 */

export function elMessage(options){
  if (currentMessage) {
    currentMessage.close();
  }
  currentMessage = Message(options);
}

['error', 'success', 'info', 'warning'].forEach((type) => {
  elMessage[type] = (options) => {
    if (typeof options === 'string') {
      options = {
        message: options,
      };
    }
    options.type = type;
    return elMessage(options);
  };
});

/*
 * 防止重复弹出MessageBox
 * @param {Object} config - MessageBox默认的配置参数
 * @param {function} callback 回调函数
 */

export function elMessageBox(config, closeBack) {
  if (currentMessageBox && currentMessageBox.close) {
    currentMessageBox.close();
  }
  currentMessageBox = MessageBox({
    ...config,
    callback() {
      if (closeBack) {
        closeBack();
      }
      currentMessageBox = null;
    },
  });
}

/*
 * 防止重复弹出Notification
 * @param {Object} config - Notification默认的配置参数
 * @param {function} callback 回调函数
 */

export function elNotification(config, closeBack) {
  // 关闭之前的Notification
  if (currentNotification && currentNotification.close) {
    currentNotification.close();
  }
  // 新创建一个Notification
  currentNotification = Notification({
    ...config,
    // 关闭后,执行回调函数,关闭后重置currentNotification为null
    onClose() {
      if (closeBack) {
        closeBack();
      }
      currentNotification = null;
    },
  });
}

二、全局引入

import { elMessage,elMessageBox,elNotification } from '@/utils/el-hint';
// 国际化
import i18n from './lang/index';
// element-ui
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

Vue.use(ElementUI, {
  size: 'medium',
  i18n: (key, value) => i18n.t(key, value)
});
// 一定要放在引入ElementUI,否则不生效
Vue.prototype.$message = elMessage; //设置全局
Vue.prototype.$elMessageBox = elMessageBox; //设置全局
Vue.prototype.$elNotification = elNotification; //设置全局

三、使用

this.$message.success('1111');

this.$elMessageBox({
	title:'标题',
	message:'111',
	showClose:false,
},() => {
	// 关闭时的回调
});

this.$elNotification({
	title:'111',
	message:'222',
	type:'error'
},() => {
	// 关闭时的回调
});

http://www.kler.cn/news/363179.html

相关文章:

  • Stable Diffusion 3.5 震撼发布!最新开源 AI 图像生成模型,艺术创作必备神器!
  • 自动化数据库管理:如何通过存储过程动态创建 MySQL 对象
  • WPF中的Style如何使用
  • 如何使用的是github提供的Azure OpenAI服务
  • React 分装webSocket
  • 文通车牌识别相机在工地称重应用中的卓越表现
  • Flutter仿京东商城APP底部导航实现
  • 代码随想录训练营Day06 | 454.四数相加II - 383. 赎金信 - 15. 三数之和 - 18. 四数之和
  • 毕业设计选题:基于django+vue的个人博客系统设计与开发
  • Github 2024-10-17 Go开源项目日报 Top10
  • 华为Eth-Trunk级联堆叠接入IPTV网络部署案例
  • 网络安全:数字世界的护卫
  • 访问kerberos认证华为的kafka集群
  • 【Python爬虫实战】使用BeautifulSoup和Scrapy抓取网页数据!
  • 大模型进阶微调篇(二):基于人类反馈的强化学习RLHF原理、优点介绍,但需要警惕LLMs的拍马屁行为
  • 2000-2023年上市公司绿色专利申请授权面板数据
  • 最近本地vmware workstation虚拟机用了一段时间后就出现网络很慢,登录不了的现象
  • 教育技术的未来:Spring Boot在线教学平台
  • SwitchHosts快速修改host文件
  • 数据资产入表:政策与实践全面解读
  • lego-loam featureAssociation 源码注释(二)
  • 嵌入式-ftrace
  • 【微信小程序_18_WXS脚本】
  • CSS学习(Grid布局和flex布局比较)
  • SDK下载依赖到IDEA的详细指南
  • ctfshow-文件上传-151-161