vue 使用element-ui中的Notification自定義按鈕并實現關閉功能及如何處理多個通知
vue 使用element-ui中的Notification自定義按鈕并實現關閉功能及如何處理多個通知
使用element-ui中的Notification,只有一個message屬性是有很大的操作空間,其余的都是寫死的,無法進行擴展,達不到想要的效果。所以只能在message上下功夫。
在element-ui官方文檔中可以看到Notification中的message屬性是可以處理VNode的所以我們可以使用VNode來達到我們需要的效果。
如何關閉通知呢?
當創(chuàng)建通知的時候,會返回該通知的實例,通過該實例的close方法可以將通知關閉。
那么當有多個通知顯示在屏幕上時,如何關閉特定彈窗的呢?
創(chuàng)建一個字典,字典的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 element-ui el-table組件自定義合計(summary-method)的坑
- 圖文詳解Element-UI中自定義修改el-table樣式
- elementui使用el-upload組件如何實現自定義上傳
- ElementUI中利用table表格自定義表頭Tooltip文字提示
- Vue Element UI自定義描述列表組件
- Vue+element-ui添加自定義右鍵菜單的方法示例
- Element-ui樹形控件el-tree自定義增刪改和局部刷新及懶加載操作
- Vue+Element UI+vue-quill-editor富文本編輯器及插入圖片自定義
- vue elementUI table 自定義表頭和行合并的實例代碼
- element-ui 的el-button組件中添加自定義顏色和圖標的實現方法
- 詳解element ui 添加自定義方法
相關文章
基于Vue-Cli 打包自動生成/抽離相關配置文件的實現方法
基于Vue-cli 項目產品部署,涉及到的交互的地址等配置信息,每次都要重新打包才能生效,極大的降低了效率。這篇文章主要介紹了基于Vue-Cli 打包自動生成/抽離相關配置文件 ,需要的朋友可以參考下2018-12-12
Vue + Webpack + Vue-loader學習教程之相關配置篇
這篇文章主要介紹了關于Vue + Webpack + Vue-loader的相關配置篇,文中通過示例代碼介紹的非常詳細,相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-03-03
vue+element+springboot實現文件下載進度條展現功能示例
本文主要介紹了vue + element-ui + springboot 實現文件下載進度條展現功能,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11

