全局重写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: '操作成功!'});
鉴定完毕,欢迎友友们一起交流学习!!
本文来自博客园,作者:红石榴21,转载请注明原文链接:https://www.cnblogs.com/liushihong21/p/17545474.html
分类:
Vue
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构