全局重写Element UI中的Message消息提示显示时长

需求:Message消息提示显示时长过长

环境:"vue": "2.6.12"、"element-ui": "^2.15.6"等

解决步骤:

1、在项目中找到main.js 文件

2、引人下面两个文件

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
 
3、重写$message方法
复制代码
 1 const messages = ['success', 'warning', 'info', 'error'];
 2 messages.forEach(type => {
 3     ElementUI.Message[type] = options => {
 4         if(typeof options === 'string') {
 5             options = {
 6                 message: options
 7             };
 8             // 默认设置
 9             options.duration = 1000; // 默认值3000毫秒
10             options.showClose = true; // 默认值false
11             options.offset = 78; // 默认值20
12         }
13         options.type = type;
14         return ElementUI.Message(options);
15     }
16 });
17 Vue.use(ElementUI);
复制代码

4、项目中调用此方法

this.$message.success('操作成功!');

坑点:在项目中需要都修改成上面的写法,避免如下的写法,不然会导致配置不生效。

 1 this.$message({ type: 'success', message: '操作成功!'}); 

鉴定完毕,欢迎友们一起交流学习!!

posted @   红石榴21  阅读(1090)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示