這篇文章給大家分享的是有關(guān)ElementUI中如何實(shí)現(xiàn)Message功能的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

在最近項(xiàng)目開(kāi)發(fā)中,接口錯(cuò)誤信息是在攔截器統(tǒng)一處理,在一次產(chǎn)品大大驗(yàn)收過(guò)程中,由于服務(wù)器沒(méi)有重啟完成,導(dǎo)致前端彈出一推錯(cuò)誤提示語(yǔ),產(chǎn)品大大對(duì)于提示語(yǔ)的交互效果提出了一系列的建議。由于項(xiàng)目使用了ElementUI框架,加上本人喜歡投(xin)機(jī)(shou)取(nian)巧(lai),于是去查看ElementUI Message的源碼,根據(jù)實(shí)際需求自定義了Message功能。
場(chǎng)景描述
場(chǎng)景一:限制頁(yè)面同時(shí)展示消息提示語(yǔ)的大數(shù)量(優(yōu)先展示后插入的提示語(yǔ))
場(chǎng)景二:根據(jù)不同情況可以?xún)?yōu)先顯示新/舊消息提示語(yǔ)
場(chǎng)景三:如果超出了大顯示數(shù)量,則剩余的消息以隊(duì)列的顯示依次展示
實(shí)現(xiàn)方案
場(chǎng)景一
功能描述
根據(jù)設(shè)置的大數(shù)量,如果存儲(chǔ)的實(shí)例列表instances長(zhǎng)度超出大限制數(shù)則銷(xiāo)毀之前的消息實(shí)例instance(調(diào)用Message方法創(chuàng)建消息提示語(yǔ)會(huì)返回當(dāng)前消息的一個(gè)實(shí)例),否則保存新建實(shí)例instance到實(shí)例列表instances中
如果消息提示語(yǔ)消失,需要從實(shí)例列表instances中移除當(dāng)前實(shí)例instance,確保頁(yè)面顯示消息數(shù)量與instances列表長(zhǎng)度統(tǒng)一
代碼實(shí)現(xiàn)
新建ZMessage構(gòu)造函數(shù)import { Message } from 'element-ui'
function ZMessage (options) {
if (!(this instanceof ZMessage)) {
return new ZMessage(options)
}
this.init(options)
}靜態(tài)配置項(xiàng)和實(shí)例列表
ZMessage.config = {
max: 0, // 大顯示數(shù)
}
ZMessage.instances = [] // 消息體實(shí)例列表定義創(chuàng)建消息和監(jiān)聽(tīng)實(shí)例消失事件方法
ZMessage.prototype.setMessage = function (options) {
const instance = Message(options)
// 監(jiān)聽(tīng)消息消失事件,從實(shí)例列表移除當(dāng)前消息實(shí)例
instance.$watch('visible', val => {
ZMessage.instances = ZMessage.instances.filter(item => item !== instance)
})
ZMessage.instances.push(instance)
}定義移除消息實(shí)例方法
ZMessage.prototype.prototype.removeMessages = function () {
const {
instances,
config: { max }
} = ZMessage
ZMessage.instances = instances.filter((instance, index) => {
if (index < instances.length - max + 1) {
instance && instance.close()
return false
}
return true
})
}初始化消息
ZMessage.prototype.init = function (options) {
const { max } = ZMessage.config
// 判斷如果超出大消息數(shù)時(shí),刪除消息
if (max > 0 && ZMessage.instances.length >= max) {
this.removeMessages() :
}
if (ZMessage.instances.length < max || !max) {
this.setMessage(options)
}
}場(chǎng)景二
功能描述
在場(chǎng)景一的基礎(chǔ)上新增優(yōu)先取消息還是舊消息的標(biāo)志操作
代碼實(shí)現(xiàn)
靜態(tài)配置項(xiàng)和實(shí)例列表
ZMessage.config = {
max: 0, // 大顯示數(shù)
showNewest: true // 是否后添加的消息覆蓋前面的消息
}初始化
ZMessage.prototype.init = function (options) {
const { max, showNewest } = ZMessage.config
// 判斷如果超出大消息數(shù)時(shí),刪除消息
if (max > 0 && ZMessage.instances.length >= max && showNewest) {
this.removeMessages()
}
if (ZMessage.instances.length < max || !max) {
this.setMessage(options)
}
}場(chǎng)景三
功能描述
在場(chǎng)景一場(chǎng)景二基礎(chǔ)上添加是否使用隊(duì)列方式存儲(chǔ)未展示消息的實(shí)例,如果超出了大限制數(shù)則創(chuàng)建消息實(shí)例的容器存儲(chǔ)到消息隊(duì)列queue中
監(jiān)聽(tīng)是否有消息消失,如果有則從消息隊(duì)列queue中取出第一個(gè)容器,創(chuàng)建消息實(shí)例
代碼實(shí)現(xiàn)
靜態(tài)配置項(xiàng)和消息容器隊(duì)列
ZMessage.config = {
max: 0, // 大顯示數(shù)
showNewest: true, // 是否后添加的消息覆蓋前面的消息
isQueue: false // 是否以隊(duì)列形式存儲(chǔ)為展示消息
}
ZMessage.queue = [] // 未展示數(shù)據(jù)的消息容器隊(duì)列生成隊(duì)列
// 生成隊(duì)列元素,延遲執(zhí)行
ZMessage.prototype.saveToQueue = function (options) {
return () => {
this.setMessage(options)
}
}初始化
// 初始化
ZMessage.prototype.init = function (options) {
const { max, isQueue, showNewest } = ZMessage.config
// 判斷如果超出大消息數(shù)時(shí),刪除消息
if (max > 0 && ZMessage.instances.length >= max && showNewest && !isQueue) {
this.removeMessages()
}
if (ZMessage.instances.length >= max && isQueue) {
// 添加隊(duì)列元素
ZMessage.queue.push(this.saveToQueue(options))
} else if (ZMessage.instances.length < max || !max) {
this.setMessage(options)
}
}獲取消息實(shí)例和添加事件監(jiān)聽(tīng)
// 獲取消息實(shí)例和添加事件監(jiān)聽(tīng)
ZMessage.prototype.setMessage = function (options) {
const instance = Message(options)
// 監(jiān)聽(tīng)消息消失事件,從實(shí)例列表移除當(dāng)前消息實(shí)例
instance.$watch('visible', val => {
ZMessage.instances = ZMessage.instances.filter(item => item !== instance)
if (ZMessage.config.isQueue && ZMessage.queue.length) {
ZMessage.queue.shift()()
}
})
ZMessage.instances.push(instance)
}最后一步
添加不同消息類(lèi)型功能靜態(tài)方法
const messageTypes = ['success', 'warning', 'error', 'info']
// 各消息類(lèi)型靜態(tài)方法
messageTypes.forEach(type => {
ZMessage[type] = options => {
let opts = options
if (typeof options === 'string') {
opts = {
message: options
}
}
return new ZMessage({ ...opts, type })
}
})完整代碼
// ZMessage.js
import { Message } from 'element-ui'
const messageTypes = ['success', 'warning', 'error', 'info']
function ZMessage (options) {
if (!(this instanceof ZMessage)) {
return new ZMessage(options)
}
this.init(options)
}
ZMessage.queue = [] // 未展示數(shù)據(jù)的消息隊(duì)列
ZMessage.instances = [] // 消息體實(shí)例列表
// 配置項(xiàng)
ZMessage.config = {
max: 0, // 大顯示數(shù)
isQueue: false, // 是否以隊(duì)列形式存儲(chǔ)為展示消息
showNewest: true // 是否后添加的消息覆蓋前面的消息
}
// 配置參數(shù)
ZMessage.setConfig = function (config = {}) {
ZMessage.config = { ...ZMessage.config, ...config }
}
ZMessage.close = Message.close
ZMessage.closeAll = Message.closeAll
// 各消息類(lèi)型靜態(tài)方法
messageTypes.forEach(type => {
ZMessage[type] = options => {
let opts = options
if (typeof options === 'string') {
opts = {
message: options
}
}
return new ZMessage({ ...opts, type })
}
})
// 初始化
ZMessage.prototype.init = function (options) {
const { max, isQueue, showNewest } = ZMessage.config
// 判斷如果超出大消息數(shù)時(shí),刪除消息
if (max > 0 && ZMessage.instances.length >= max && showNewest && !isQueue) {
this.removeMessages()
}
if (ZMessage.instances.length >= max && isQueue) {
// 添加隊(duì)列元素
ZMessage.queue.push(this.saveToQueue(options))
} else if (ZMessage.instances.length < max || !max) {
this.setMessage(options)
}
}
// 移除消息
ZMessage.prototype.removeMessages = function () {
const {
instances,
config: { max }
} = ZMessage
ZMessage.instances = instances.filter((instance, index) => {
if (index < instances.length - max + 1) {
instance && instance.close()
return false
}
return true
})
}
// 獲取消息實(shí)例和添加事件監(jiān)聽(tīng)
ZMessage.prototype.setMessage = function (options) {
const instance = Message(options)
// 監(jiān)聽(tīng)消息消失事件,從實(shí)例列表移除當(dāng)前消息實(shí)例
instance.$watch('visible', val => {
ZMessage.instances = ZMessage.instances.filter(item => item !== instance)
if (ZMessage.config.isQueue && ZMessage.queue.length) {
ZMessage.queue.shift()()
}
})
ZMessage.instances.push(instance)
}
// 生成隊(duì)列元素,延遲執(zhí)行
ZMessage.prototype.saveToQueue = function (options) {
return () => {
this.setMessage(options)
}
}
export default ZMessage
// 使用方式
import Vue from 'vue'
import ZMessage from 'path/to/ZMessage.js'
// 引入Element
// ....
// 自定義配置項(xiàng)
ZMessage.setConfig({ max: 1, isQueue: false, showNewest: true })
// 覆蓋默認(rèn)$message
Vue.prototype.$message = ZMessage感謝各位的閱讀!關(guān)于“ElementUI中如何實(shí)現(xiàn)Message功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
分享名稱(chēng):ElementUI中如何實(shí)現(xiàn)Message功能-創(chuàng)新互聯(lián)
文章來(lái)源:http://chinadenli.net/article42/gcshc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、商城網(wǎng)站、網(wǎng)站策劃、軟件開(kāi)發(fā)、小程序開(kāi)發(fā)、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容