当前位置: 首页 > java >正文

uniapp中uni.showToast和 uni.showLoading同时使用时出现提示中断冲突问题。

以下代码实现了一个增强版的加载状态管理器,主要功能包括Loading状态管理和Toast队列管理,解决了Loading和Toast冲突的问题。

/*** 加载状态管理器(增强版)* 新增特性:* 1. Toast队列管理* 2. 自动处理Loading和Toast的冲突*/
export const loadingManager = {_isLoading: false,_currentLoading: null,_toastQueue: [], // 待显示的Toast队列/*** 显示加载提示* @param {String} title 提示文字*/show: function(title = '加载中...') {if (this._isLoading) return;try {// 清除当前可能存在的Toastuni.hideToast();this._currentLoading = uni.showLoading({title,mask: true});this._isLoading = true;} catch (e) {console.error('showLoading error:', e);}},/*** 隐藏加载提示* 会自动处理待显示的Toast*/hide: function() {if (!this._isLoading) return;try {uni.hideLoading();} catch (e) {console.warn('hideLoading error:', e);} finally {this._isLoading = false;this._currentLoading = null;// 处理队列中的Toastthis._processToastQueue();}},/*** 切换加载状态* @param {Boolean} loading 是否显示加载* @param {String} title 提示文字*/toggle: function(loading, title) {loading ? this.show(title) : this.hide();},/*** 内部方法:处理Toast队列*/_processToastQueue: function() {if (this._toastQueue.length > 0) {const nextToast = this._toastQueue.shift();setTimeout(() => {uni.showToast(nextToast);// 递归处理剩余Toastthis._processToastQueue();}, 350); // 适当延迟确保UI过渡}},/*** 安全显示Toast(新增方法)* @param {Object} options Toast配置*/safeToast: function(options) {if (this._isLoading) {// 如果正在loading,将Toast加入队列this._toastQueue.push(options);} else {// 直接显示Toastuni.showToast(options);}}
};/*** 增强版Toast提示* 自动处理与Loading的冲突* @param {Object} options 配置对象*/
export const toast = (options) => {const config = {icon: 'none',duration: 2000,mask: false,...options};// 使用安全版ToastloadingManager.safeToast(config);
};

使用示例

// 引入后再使用
// 显示加载提示
loadingManager.show();
//  隐藏加载提示
loadingManager.hide();
// 显示提示toast({//传入配置对象title: '提示内容'});
http://www.xdnf.cn/news/18043.html

相关文章:

  • java设计模式之迪米特法则使用场景分析
  • 佳文赏读 || (CVPR 2025新突破) Robobrain:机器人操作从抽象到具体的统一大脑模型(A Unified Brain Model)
  • 魔搭api功能优化
  • 栈与队列:数据结构中的双生子
  • 【P14 3-6 】OpenCV Python——视频加载、摄像头调用、视频基本信息获取(宽、高、帧率、总帧数),视频保存在指定位置
  • 在职老D渗透日记day18:sqli-labs靶场通关(第26关)get报错注入 过滤or和and基础上又过滤了空格和注释符 ‘闭合 手动注入
  • qt vs2019编译QXlsx
  • Linux软件编程(六)(exec 函数族、system 实现、进程回收与线程通信)
  • C++ 内存管理(内存分布 , 管理方式 , new和delete实现原理)
  • pidgen!DecodeProdKey函数分析之iDecodedBytesMax
  • 服务器硬件电路设计之I2C问答(七):解析 I2C 通信 “卡壳” 难题:从设备无响应与跨电压兼容之道
  • Spring AI Starter和文档解读
  • InfluxDB 数据迁移工具:跨数据库同步方案(一)
  • 超酷炫的Three.js示例
  • Java:File类、递归、字符集、IO流体系及Commons-io框架
  • 【大模型核心技术】Dify 入门教程
  • Flow-GRPO:通过在线 RL 训练 Flow matching 模型
  • PS插件整合包!内置数百款PS插件,支持PS2017-PS2025所有版本!
  • 【学习嵌入式day-26-线程间通信】
  • TypeScript快速入门
  • CPP多线程3:async和future、promise
  • ArrayList的contains问题
  • 机器学习 [白板推导](十二)[卡曼滤波、粒子滤波]
  • 第G7周:Semi-Supervised GAN 理论与实战
  • 【科研绘图系列】R语言绘制雷达图
  • 洛谷B3865 [GESP202309 二级] 小杨的 X 字矩阵(举一反三)
  • 从 MySQL 5.7 迁移到 8.0:别让 SQL 文件 “坑” 了你
  • 《从入门到高可用:2025最新MySQL 8.0全栈速通指南》
  • Linux配置Dante使用的pam验证
  • 【攻防实战】红队攻防之Goby反杀