正確使用element
中Notification
的回調(diào)方法
經(jīng)常的用的是onClose(關(guān)閉notification)
和onClick(點(diǎn)擊notification)
闻鉴,
注意:
onClick: this.function()
和onClick() { this.function() }
的區(qū)別就行了茵乱,其他正常寫就ok
對(duì)于
close
,API也有講到:調(diào)用Notification
或this.$notify
會(huì)返回當(dāng)前Notification
的實(shí)例孟岛。如果需要手動(dòng)關(guān)閉實(shí)例瓶竭,可以調(diào)用它的close
方法,按照API寫就行渠羞。
onClick
需求一般要么是點(diǎn)擊通知執(zhí)行方法斤贰、要么是點(diǎn)擊某個(gè)地方執(zhí)行方法,我的需求是多個(gè)通知次询,點(diǎn)擊單個(gè)通知的某個(gè)位置跳轉(zhuǎn)且關(guān)閉
for (let i = 0; i < showList.length; i++) {
let notify = await this.$notify({//解決消息框重疊 也可以通過加延遲解決
title: showList[i].message.title,
message: h('p', [
h('p', null, showList[i].message.content),
h('p', {
class: 'notice-cursor',
on: {
click: () => {
this.closeNotification(showList[i].messageId)
},
},
}, '跳轉(zhuǎn)列表查看'),
]),
dangerouslyUseHTMLString: true,
duration: 10000,
position: 'bottom-right',
customClass: 'home-notice',
offset: 20,
onClose() {
console.log('關(guān)閉回調(diào)')
}
})
this.notifications[showList[i].messageId] = notify
}
//關(guān)閉單個(gè)通知
closeNotification(messageId){
//省略部分代碼
this.notifications[messageId].close()
delete this.notifications[messageId]
},
如果是點(diǎn)擊通知塊執(zhí)行荧恍,直接把方法,寫在onClick
回調(diào)中就可以了屯吊,注意this指向块饺,即:
onClick() {
vm.closeNotification(showList[i].messageId)
}
關(guān)閉所有通知
//關(guān)閉所有通知
closeAllNotification() {
for (let key in this.notifications) {
this.notifications[key].close()
delete this.notifications[key]
}
},