温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

vue使用element-ui中的Notification自定义按钮并实现关闭功能的案例分析

发布时间:2021-02-07 13:00:07 阅读:1845 作者:小新 栏目:web开发
Vue开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章主要介绍vue使用element-ui中的Notification自定义按钮并实现关闭功能的案例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

vue 使用element-ui中的Notification自定义按钮并实现关闭功能及如何处理多个通知

使用element-ui中的Notification,只有一个message属性是有很大的操作空间,其余的都是写死的,无法进行扩展,达不到想要的效果。所以只能在message上下功夫。

在element-ui官方文档中可以看到Notification中的message属性是可以处理VNode的所以我们可以使用VNode来达到我们需要的效果。

如何关闭通知呢?

当创建通知的时候,会返回该通知的实例,通过该实例的close方法可以将通知关闭。

那么当有多个通知显示在屏幕上时,如何关闭特定弹窗的呢?

创建一个字典,字典的key是message的Id,value是显示该消息的通知的实例。从而可以关闭特定的通知。代码如下。

import mainTable from './mixin/mainTable';
import systemMenu from './template/system-menu';
import headerRow from './template/header';
export default {
  name'xxxxx',
  data() {
    return {
      //使用messageId作为弹窗的key,用来获取弹窗的实例,以对对应弹窗进行操作
      notifications: {}
    };
  },
  mounted() {
    this.$messageWebsocket.websocketApi.initWebSocket(this.$store.state.login.userInfo.userInfo.idthis.openMessageTips);
  },
  methods: {
    //关闭单个通知
    closeNotification(id, operateCode, message){
      this.notifications[message.messageId].close();
      delete this.notifications[message.messageId];
    },
    //关闭所有通知
    closeAllNotification(){
      let _this = this;
      for (let key in _this.notifications) {
        _this.notifications[key].close();
        delete _this.notifications[key];
      }
    },
    //打开一个新的通知
    openMessageTips(message){
      let _this = this;
      this.closeAllNotification();
      let notify = this.$notify({
        title'三高协诊消息',
        position'bottom-right',
        showClosefalse,
        dangerouslyUseHTMLStringtrue,
        messagethis.$createElement('div'null,
          [
            this.$createElement('div'null, [this.$createElement('span'null, message.content)]),
            this.$createElement('div'null,
              [
                this.$createElement(
                  'button',
                  {
                    style: {
                      padding'10px 18px',
                      margin'10px 12px 0px 2px',
                      textAlign'center',
                      textDecoration'none',
                      display'inline-block',
                      webkitTransitionDuration'0.4s',
                      transitionDuration'0.4s',
                      cursor'pointer',
                      backgroundColor'white',
                      color'black',
                      border'2px solid #e7e7e7',
                    },
                    on: {
                      mouseoutfunction(e){
                        e.target.style.backgroundColor = 'white';
                      },
                      mouseoverfunction(e){
                        e.target.style.backgroundColor = '#e7e7e7'
                      },
                      click: _this.closeNotification.bind(_this, 11, message)
                    }
                  },
                  "查看"
                ),
                this.$createElement(
                  'button',
                  {
                    style: {
                      padding'10px 18px',
                      margin'10px 2px 0px 12px',
                      textAlign'center',
                      textDecoration'none',
                      display'inline-block',
                      webkitTransitionDuration'0.4s',
                      transitionDuration'0.4s',
                      cursor'pointer',
                      backgroundColor'white',
                      color'black',
                      border'2px solid #e7e7e7',
                    },
                    on: {
                      //鼠标移出的回调
                      mouseoutfunction(e){
                        e.target.style.backgroundColor = 'white';
                      },
                      //鼠标移入的回调
                      mouseoverfunction(e){
                        e.target.style.backgroundColor = '#e7e7e7'
                      },
                      click: _this.closeNotification.bind(_this, 12, message)
                    }
                  },
                  "稍后提醒(五分钟后)"
                )
              ]
            )
          ]
        ),
        duration0,
      });
      //将messageId和通知实例放入字典中
      this.notifications[message.messageId] = notify;
    }
  }
};

以上是“vue使用element-ui中的Notification自定义按钮并实现关闭功能的案例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×