使用element-ui中的Notification,只有一个message属性是有很大的操作空间,其余的都是写死的,无法进行扩展,达不到想要的效果。所以只能在message上下功夫。
在element-ui官方文档中可以看到Notification中的message属性是可以处理VNode的所以我们可以使用VNode来达到我们需要的效果。
如何关闭通知呢?
当创建通知的时候,会返回该通知的实例,通过该实例的close方法可以将通知关闭。
那么当有多个通知显示在屏幕上时,如何关闭特定弹窗的呢?
创建一个字典,字典的key是message的Id,value是显示该消息的通知的实例。从而可以关闭特定的通知。代码如下。
1import mainTable from './mixin/mainTable'; 2import systemMenu from './template/system-menu'; 3import headerRow from './template/header'; 4 5export default { 6 name: 'xxxxx', 7 data() { 8 return { 9 //使用messageId作为弹窗的key,用来获取弹窗的实例,以对对应弹窗进行操作 10 notifications: {} 11 }; 12 }, 13 mounted() { 14 this.$messageWebsocket.websocketApi.initWebSocket(this.$store.state.login.userInfo.userInfo.id, this.openMessageTips); 15 }, 16 methods: { 17 //关闭单个通知 18 closeNotification(id, operateCode, message){ 19 this.notifications[message.messageId].close(); 20 delete this.notifications[message.messageId]; 21 }, 22 23 //关闭所有通知 24 closeAllNotification(){ 25 let _this = this; 26 for (let key in _this.notifications) { 27 _this.notifications[key].close(); 28 delete _this.notifications[key]; 29 } 30 }, 31 32 //打开一个新的通知 33 openMessageTips(message){ 34 let _this = this; 35 this.closeAllNotification(); 36 let notify = this.$notify({ 37 title: '消息', 38 position: 'bottom-right', 39 showClose: false, 40 dangerouslyUseHTMLString: true, 41 message: this.$createElement('div', null, 42 [ 43 this.$createElement('div', null, [this.$createElement('span', null, message.content)]), 44 this.$createElement('div', null, 45 [ 46 this.$createElement( 47 'button', 48 { 49 style: { 50 padding: '10px 18px', 51 margin: '10px 12px 0px 2px', 52 textAlign: 'center', 53 textDecoration: 'none', 54 display: 'inline-block', 55 webkitTransitionDuration: '0.4s', 56 transitionDuration: '0.4s', 57 cursor: 'pointer', 58 backgroundColor: 'white', 59 color: 'black', 60 border: '2px solid #e7e7e7', 61 }, 62 on: { 63 mouseout: function(e){ 64 e.target.style.backgroundColor = 'white'; 65 }, 66 mouseover: function(e){ 67 e.target.style.backgroundColor = '#e7e7e7' 68 }, 69 click: _this.closeNotification.bind(_this, 1, 1, message) 70 } 71 }, 72 "查看" 73 ), 74 this.$createElement( 75 'button', 76 { 77 style: { 78 padding: '10px 18px', 79 margin: '10px 2px 0px 12px', 80 textAlign: 'center', 81 textDecoration: 'none', 82 display: 'inline-block', 83 webkitTransitionDuration: '0.4s', 84 transitionDuration: '0.4s', 85 cursor: 'pointer', 86 backgroundColor: 'white', 87 color: 'black', 88 border: '2px solid #e7e7e7', 89 }, 90 on: { 91 //鼠标移出的回调 92 mouseout: function(e){ 93 e.target.style.backgroundColor = 'white'; 94 }, 95 //鼠标移入的回调 96 mouseover: function(e){ 97 e.target.style.backgroundColor = '#e7e7e7' 98 }, 99 click: _this.closeNotification.bind(_this, 1, 2, message) 100 } 101 }, 102 "稍后提醒(五分钟后)" 103 ) 104 ] 105 ) 106 ] 107 ), 108 duration: 0, 109 }); 110 //将messageId和通知实例放入字典中 111 this.notifications[message.messageId] = notify; 112 } 113 } 114};