您好!欢迎来到雅安论坛
关注我们
扫码关注官方微信
手机版
手机扫描直接访问
欢迎: 附近的朋友。
热搜:    
111111

【Ant Design of Vue】可手动关闭Message 全局提示

[复制链接]
本站网友  发表于 2023-10-3 19:34:26 |阅读模式 打印 上一主题 下一主题
【Ant Design of Vue】可手动关闭Message 全局提示

将以下代码封装到 message.js 里
  1. // 实现Message全局提示可手动关闭
  2. import { message } from 'ant-design-vue';
  3. /*
  4.         h: 是content自带的参数,表示createElement
  5.         content:文本内容
  6. */
  7. const createElementVNode = (h, content) => {
  8.     // 延用的官方的文本内容及样式
  9.     const innerText = h('span', { style: { color: '#4a5970', fontSize: '14px' } }, content);
  10.     // 自定义关闭图标
  11.     const innerIcon = h('a-icon', {
  12.         style: { marginLeft: '10px', color: '#1890ff', cursor: 'pointer' },
  13.         attrs: { type: 'close-circle' },
  14.         on: { click: () => message.destroy() },
  15.     });
  16.      // 合并到一个VNode节点中
  17.     const container = h('span', {}, [innerText, innerIcon]);
  18.     return container;
  19. };
  20. // 由于message有多种状态, 这里需要额外处理
  21. const dtMessage = {};
  22. // 给dtMessage循环赋值不同类型的message函数
  23. ['error', 'success', 'info', 'warning', 'warn'].forEach((type) => {
  24.     // 处理message[type](config)参数为对象类型
  25.     dtMessage[type] = (content, duration, onClose) => {
  26.         if (typeof content === 'object') {
  27.             return message[type]({ ...content, content: h => createElementVNode(h, content?.content) });
  28.         }
  29.         return message[type](h => createElementVNode(h, content), duration, onClose);
  30.     };
  31. });
  32. export default dtMessage;
复制代码
  1. // 按需加载antdvue组件的文件(lazy_use.js)

  2. // 引入message.js
  3. import message from '@/core/message.js'
  4. import Vue from 'vue'
  5. // 定义Vue实例的原型属性,使每个Vue实例中都可以用,又不会污染全局作用域
  6. Vue.prototype.$message = message
复制代码
使用
  1. <template>
  2.   <a-button type="primary" @click="onClick">点击弹出message</a-button>
  3. </template>

  4. <script>
  5. export default {
  6.   methods: {
  7.     onClick() {
  8.       this.$message.success('This is a success message');
  9.       this.$message.error('This is an error message');
  10.       this.$message.warning('This is a warning message');
  11.     },
  12.   },
  13. };
  14. </script>

  15. <style></style>
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

关注3

粉丝13

帖子2251

发布主题
阅读排行 更多
广告位
欢迎使用无需登录在线留言
姓名 

手机 

内容
关注我们
  • 关注官方微信

  • 论坛QQ群

Sitemap小黑屋Archiver雅安论坛( 已经安全运行 ) |网站地图

在线客服

会员收集的资源(插件源码等),仅方便学习,您需要自行承担版权风险,未获得原作者授权的情况下,请勿将文章公开发布或用于商业用途。

声明:本站内容来自于论坛作者本人的观点,不代表本网站的观点和看法,与本网站立场无关,相关责任作者自负。如有侵权,请联系我们及时删除

高能预警:本站为个人网站,非团队运作,常年务农,山上信号不好,在线少,如有不妥之处,望大佬们多多包涵,万分感激!

雅安论坛互联网违法和不良信息举报平台 您也可以通过留言进行相关问题提交,留言提交无需注册,我们将严格保证用户隐私。   © 2009-2021