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;
        }
    }
};

原文地址:https://www.cnblogs.com/zxb1996/p/notification_question1.html

时间: 2024-08-25 14:31:20

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

关于vue.js element ui 表单验证 this.$refs[formName].validate()的问题

方法使用前需了解: 来自”和“小编的小提示: 首先打印一下this.$refs[formName],检查是否拿到了正确的需要验证的form. 其次在拿到了正确的form后,检查该form上添加的表单验证是否正确,需要注意的点有: 1.使用此方法前检查prop一定必须要写在<el-form-item>上面,写在里面的input上或者其他任何地方都不行(el-form-item prop属性绑定) 2.el-form rules,model属性绑定,ref标识 自定义表单验证的坑: 一.valid

Vue框架Element UI教程(二)

原文:https://www.jianshu.com/p/1704b5935a8 [时间选择器] Element UI手册:https://cloud.tencent.com/developer/doc/1270 中文文档:http://element-cn.eleme.io/#/zh-CN github地址:https://github.com/ElemeFE/element 前一篇已经安装好了Element UI环境,现在开始来实际操作框架提供的一些组件的运用了. 在准备好以下文章里面的内容

net MVC +Vue.js+Element UI 笔记

最近项目需求要用到Vue 与 Element UI.但是又不想用Node.js.就结合了Net.MVC来做项目开发.故而做个笔记来记录一些遇到的问题和处理思路 用到MVC主要考虑是到 对 Node.js 不是特别了解.不想给自己埋坑. 而且基于MVC的话,能用到MVC的服务器端渲染,Session,验证,路由机制等等还是非常和嗨皮的. 然而集合MVC的话,比较烦的是在做可复用Component和JS文件的封装上没Node.js那么方便. Note:不得不吐槽,Vue.js简直和Flash Fle

Android自定义按钮实现长按功能

通过自定义BUTTON,写一个LongTouchBtn类,在按下的时候执行onTouchEvent事件,通过这个事件使用回调函数来实现长按功能! XML: <huahua.btnlongtouch.LongTouchBtn android:id="@+id/btn2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text=&q

Element UI 中被隐藏的滚动条

Element UI 官网中有用到自定义的滚动条组件,但是发布的所有版本中都不曾提及,个中原因我们不得而知,不过我们还是可以拿过来引用到自己的项目中. 使用的时候,放在 <el-scrollbar></el-scrollbar> 标签内即可如: <div style="height:100%"> <el-scrollbar class="m-scroll" style="height:100%">

vue开源Element UI表单设计及代码生成器

在日常的开发工作中,表单开发是较为繁琐且重复的.本文介绍一个我自己写的,提高开发效率的小工具. 1 可视化设计器 设计器基于Element UI ,可通过点击或拖拽的方式设计基本表单, 设计器生成的代码可直接运行在基于 Element 的 vue 项目中. github仓库   https://github.com/JakHuang/form-generator 码云仓库  https://gitee.com/mrhj/form-generator 演示地址 https://mrhj.gitee

vue 集成 element ui

打开vue项目所在的项目路径 输入 npm install element-ui -S 安装element ui 表示安装成功 在vue项目下的node_modules下会显示 element ui 配置element ui 改变项目目录中的main.js文件: 初始main.js文件: import Vue from 'vue'import App from './App'import router from './router'import ElementUI from 'element-u

关于vue和element ui的国际化

因为公司需求近日一直在做国际化处理, 首先,我们用的是vue的单文件组件,实现国际化是在写好的一个文件组件里面vue_min.js里,而且它相当于一个js的模块,在每一个单文件的组件里面使用. 遇见的第一个问题国际化失败,只显示自己定义的json文件国际化.而且element ui的的国际化不显示, 解决的方案就是, 1.引入的时候应该从新引入 import enLocale from 'element-ui/lib/locale/lang/en' import zhLocale from 'e

Element ui 中使用table组件实现分页记忆选中

我们再用vue和element-ui,或者其他的表格的时候,可能需要能记忆翻页勾选,那么实现以下几个方法就ok了 首先定义个data值 data () { return { multipleSelectionAll: [], // 所有选中的数据包含跨页数据 idKey: 'personId' // 标识列表数据中每一行的唯一键的名称(需要按自己的数据改一下) } } 方法中定义以下: methods : { // 设置选中的方法 setSelectRow() { if (!this.multi