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

 更新时间:2019年08月17日 10:45:43   作者:会飞的joy  
这篇文章主要介绍了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.id, this.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',
        showClose: false,
        dangerouslyUseHTMLString: true,
        message: this.$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: {
                      mouseout: function(e){
                        e.target.style.backgroundColor = 'white';
                      },
                      mouseover: function(e){
                        e.target.style.backgroundColor = '#e7e7e7'
                      },
                      click: _this.closeNotification.bind(_this, 1, 1, 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: {
                      //鼠标移出的回调
                      mouseout: function(e){
                        e.target.style.backgroundColor = 'white';
                      },
                      //鼠标移入的回调
                      mouseover: function(e){
                        e.target.style.backgroundColor = '#e7e7e7'
                      },
                      click: _this.closeNotification.bind(_this, 1, 2, message)
                    }
                  },
                  "稍后提醒(五分钟后)"
                )
              ]
            )
          ]
        ),
        duration: 0,
      });
      //将messageId和通知实例放入字典中
      this.notifications[message.messageId] = notify;
    }
  }
};

总结

以上所述是小编给大家介绍的vue 使用element-ui中的Notification自定义按钮并实现关闭功能及如何处理多个通知,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

相关文章

  • vue中的模态对话框组件实现过程

    vue中的模态对话框组件实现过程

    这篇文章主要介绍了vue中的模态对话框组件实现过程,通过template定义组件,并添加相应的对话框样式,需要的朋友可以参考下
    2018-05-05
  • Element UI中table单元格合并的解决过程

    Element UI中table单元格合并的解决过程

    element ui中的table表格数据是动态生成的,最近遇到个需求,要求我们对单元格进行合并,所以下面这篇文章主要给大家介绍了关于Element UI中table单元格合并的解决过程,需要的朋友可以参考下
    2022-08-08
  • 详解vue-router 初始化时做了什么

    详解vue-router 初始化时做了什么

    这篇文章主要介绍了详解vue-router 初始化时做了什么,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • vue开发之不同浏览器的类型判断方式

    vue开发之不同浏览器的类型判断方式

    这篇文章主要介绍了vue开发之不同浏览器的类型判断方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • vue2.0移动端滑动事件vue-touch的实例代码

    vue2.0移动端滑动事件vue-touch的实例代码

    这篇文章主要介绍了vue2.0移动端滑动事件vue-touch的实例代码,需要的朋友可以参考下
    2018-11-11
  • Vue对Element中el-tab-pane添加@click事件无效问题解决

    Vue对Element中el-tab-pane添加@click事件无效问题解决

    这篇文章主要给大家介绍了关于Vue对Element中el-tab-pane添加@click事件无效问题的解决办法,文中通过图文以及代码示例介绍的非常详细,需要的朋友可以参考下
    2023-07-07
  • vue如何查找数组中符合条件的对象

    vue如何查找数组中符合条件的对象

    这篇文章主要介绍了vue如何查找数组中符合条件的对象,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-09-09
  • 记一次微信小程序与webviewH5通信的踩坑记录

    记一次微信小程序与webviewH5通信的踩坑记录

    uniapp开发小程序的过程中主、分包有大小限制,随着业务的增加,使用web-view加载H5的方式,往往纯加载并不能满足业务需求,这个时候就得了解小程序与H5的交互,这篇文章主要给大家介绍了关于微信小程序与webviewH5通信的踩坑记录,需要的朋友可以参考下
    2024-07-07
  • Vue+Node.js+WebSocket实现即时通讯

    Vue+Node.js+WebSocket实现即时通讯

    本文主要介绍了Vue+Node.js+WebSocket实现即时通讯,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05
  • vue.prototype和vue.use的区别和注意点小结

    vue.prototype和vue.use的区别和注意点小结

    这篇文章主要介绍了vue.prototype和vue.use的区别和注意点小结,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-04-04

最新评论