Message 消息提示每次只弹出1个,不能同时出现2个

项目场景

PC端开发,vue + elementUI

问题描述

点击 Message 消息提示同时出现2个或者多个,设置成只弹一个。

原因分析

Element UI的Message消息提示是点击一次触发一次的。


解决方案

  1. 在utils文件创建resetMessage.js
//resetMessage.js
/**重置message,防止重复点击重复弹出message消息提示 */
import {
    Message
} from 'element-ui';

let messageInstance = null;

const resetMessage = (options) => {
        if(messageInstance) {
            messageInstance.close();

        }
        Message.closeAll(); //手动关闭所有消息提示实例
        messageInstance = Message(options);
        // console.log(Message(options));

};

['error','success','info','warning'].forEach(type => {
    resetMessage[type] = options => {
        if(typeof options === 'string') {
            options = {
                message:options
            }
        }
        options.type = type
        return resetMessage(options)
    }
})

export const message = resetMessage

2.在main.js中导入,并挂载到vue全局对象上。

//main.js
//导入
import {
    message
} from '@/utils/resetMessage';



Vue.use(ElementUI)
//挂载
Vue.prototype.$message = message;

3.在组件内使用

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,这是一个比较复杂的需求,需要涉及到一些electron-dl和队列的操作。我会尽可能详细地给代码实现,但是可能会有一些需要你自己根据实际情况修改的地方。 首先,需要在渲染进程中创建一个下载队列: ```javascript const downloadQueue = []; function addToQueue(item) { downloadQueue.push(item); updateDownloadList(); } function removeFromQueue(item) { const index = downloadQueue.indexOf(item); if (index > -1) { downloadQueue.splice(index, 1); updateDownloadList(); } } function updateDownloadList() { // 渲染 downloadQueue 中的数据 } ``` 然后,在渲染进程中创建一个下载管理器,用于管理下载器的数量和状态: ```javascript class DownloadManager { constructor() { this.maxConcurrentDownloads = 5; this.activeDownloads = []; } async startDownload(item) { const download = await startDownload(item); this.activeDownloads.push(download); this.update(); download.on('done', () => { this.activeDownloads.splice(this.activeDownloads.indexOf(download), 1); this.update(); showMessage(`${item.name} 下载完成`); if (downloadQueue.length > 0) { this.startDownload(downloadQueue.shift()); } }); } pauseAll() { this.activeDownloads.forEach(download => { download.pause(); }); this.update(); } resumeAll() { this.activeDownloads.forEach(download => { download.resume(); }); this.update(); } cancelAll() { this.activeDownloads.forEach(download => { download.cancel(); }); this.activeDownloads = []; this.update(); } pause(download) { download.pause(); this.update(); } resume(download) { download.resume(); this.update(); } cancel(download) { download.cancel(); this.activeDownloads.splice(this.activeDownloads.indexOf(download), 1); this.update(); } update() { // 渲染 activeDownloads 中的数据 } } const downloadManager = new DownloadManager(); ``` 其中,startDownload 方法用于创建一个下载器并开始下载,done 事件用于在下载完成后从 activeDownloads 中删除该下载器并启动下一个任务。pauseAll、resumeAll、cancelAll 方法用于暂停、恢复、取消所有下载任务;pause、resume、cancel 方法用于暂停、恢复、取消单个下载任务。 下面是 startDownload 方法的实现: ```javascript function startDownload(item) { return new Promise((resolve, reject) => { const options = { directory: item.directory, filename: item.filename, onProgress: progress => { item.progress = progress.percent; downloadManager.update(); } }; const download = electronDl(item.url, options); download.once('done', resolve); download.once('error', reject); }); } ``` 其中,electronDl 是 electron-dl 库提供的下载函数,options 中包含了下载的目录和文件名,以及进度回调函数 onProgress。 最后是渲染进程中的代码,用于渲染下载列表和控制按钮: ```javascript function renderDownloadItem(item) { return ` <div class="download-item"> <div class="download-name">${item.name}</div> <div class="download-progress"> <Progress percent=${item.progress} /> </div> <div class="download-controls"> <Button onClick=${() => downloadManager.pause(item)}>暂停</Button> <Button onClick=${() => downloadManager.resume(item)}>恢复</Button> <Button onClick=${() => downloadManager.cancel(item)}>取消</Button> </div> </div> `; } function renderDownloadList() { const items = downloadQueue.concat(downloadManager.activeDownloads); const list = items.map(renderDownloadItem).join(''); // 渲染 list } function showMessage(message) { // 使用 ant-design 的 message提示消息 } // 绑定按钮事件 document.querySelector('#pause-all').addEventListener('click', downloadManager.pauseAll.bind(downloadManager)); document.querySelector('#resume-all').addEventListener('click', downloadManager.resumeAll.bind(downloadManager)); document.querySelector('#cancel-all').addEventListener('click', downloadManager.cancelAll.bind(downloadManager)); ``` 以上代码是一个比较简单的实现,可能还有很多细节需要你自己去完善。希望能够帮到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值