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

uniapp踩坑 uni.showToast 和 uni.showLoading

uniapp踩坑 uni.showToast 和 uni.showLoading

一、问题描述

  • uni.showLoading 和 uni.showToast 混合使用时,showLoading和showToast会相互覆盖对方,调用hideLoading时也会将toast内容进行隐藏。

二、触发条件

  • 1.uniapp中使用自己封装的axois,拦截器使用 uni.showToast 做异常信息处理
  • 2.业务中使用 uni.showLoading 做业务处理
  • 3.当请求异常被catch抓到,使用 uni.hideLoading 清除 loading 时,异常信息 toast 会被覆盖掉。

三、解决思路

  • 小程序将Toast和Loading放到同一层渲染引起的,而且缺乏一个优先级判断,也没有提供Toast、Loading是否正在显示的接口供业务侧判断。所以我们自己实现这套逻辑,判断其中有一个已经渲染,泽不执行另一个。

四、实现方案

1.封装一下toast和loading

/*** 显示消息提示框* @param title* @param options* @constructor*/
export function Toast(title: string, options?: Partial<UniApp.ShowToastOptions>) {uni.showToast({title,duration: 1500,icon: 'none',mask: true,...options,});
}/*** 隐藏消息提示框*/
export function HideToast() {uni.hideToast();
}/*** 显示 loading 提示框* @param title* @param options* @constructor*/
export function Loading(title: string, options?: Partial<UniApp.ShowLoadingOptions>) {uni.showLoading({title,mask: true,...options,});
}/*** 隐藏 loading 提示框*/
export function HideLoading() {uni.hideLoading();
}

2.要加个变量控制toast和loading的优先级,最简单就是通过vue的全局状态管理来控制

export const usePromptStore = defineStore({id: 'promptStore',state: (): IState => ({isShowLoading: false,isShowToast: false,}),getters: {getIsShowLoading: (state) => state.isShowLoading,getIsShowToast: (state) => state.isShowToast,},actions: {setIsShowLoading(val: boolean) {this.isShowLoading = val;},setIsShowToast(val: boolean) {this.isShowToast = val;},},
});

3.改造一下封装的toast和loading

/*** 显示消息提示框* @param title* @param options* @constructor*/
export function Toast(title: string, options?: Partial<UniApp.ShowToastOptions>) {const promptStore = usePromptStore();if (promptStore.disabledToast) return;if (promptStore.isShowLoading) {// Toast优先级更高HideLoading();}promptStore.setIsShowToast(true);uni.showToast({title,duration: 1500,icon: 'none',mask: true,...options,});const timer = setTimeout(() => {promptStore.setIsShowToast(false);clearTimeout(timer)}, 1500);
}/*** 隐藏消息提示框*/
export function HideToast() {const promptStore = usePromptStore();promptStore.setIsShowToast(false);uni.hideToast();
}/*** 显示 loading 提示框* @param title* @param options* @constructor*/
export function Loading(title: string, options?: Partial<UniApp.ShowLoadingOptions>) {const promptStore = usePromptStore();if (promptStore.isShowToast) {// Toast优先级更高return;}promptStore.setIsShowLoading(true);uni.showLoading({title,mask: true,...options,});
}/*** 隐藏 loading 提示框*/
export function HideLoading() {const promptStore = usePromptStore();if (promptStore.isShowToast) {// Toast优先级更高return;}promptStore.setIsShowLoading(false);uni.hideLoading();
}
http://www.lryc.cn/news/342454.html

相关文章:

  • BIGRU、CNN-BIGRU、CNN-BIGRU-ATTENTION、TCN-BIGRU、TCN-BIGRU-ATTENTION合集
  • 通过 Java 操作 redis -- 基本通用命令
  • Jenkins集成Kubernetes 部署springboot项目
  • 个股期权是什么期权?个股期权什么时候推出?
  • TCP UDP
  • PCIE协议-1
  • [C++][PCL]pcl安装包预编译包国内源下载地址
  • 海洋行业工业气体检测传感器的重要性
  • 免费在线录屏、无需注册、免费可用、无限制
  • 5V升9V2A升压恒压WT3231
  • Java中枚举类的使用详解
  • C++11 设计模式6. 建造者模式,也叫做生成器模式
  • GPS与精致农业 无人机应用 农业遥感 农业类
  • Kotlin注解简介
  • 代码随想录训练营
  • java中的变量、数据类型、人机交互
  • Python中的生成器是什么
  • 【Camera2完整流程分析四】从log角度分析CameraService启动流程
  • 基于SSM SpringBoot vue教务排课系统
  • 深入理解 LinkedList 及底层源码分析
  • 美易官方:英伟达业绩将难以撑起股价?
  • 超实用干货!FP独立站引流攻略
  • php之框架底层中间件模式开发实现、array_reduce的应用
  • fabric搭建生产网络
  • 聊聊 ASP.NET Core 中间件(二):中间件和筛选器的区别
  • Nginx配置Https缺少SSL模块
  • 超详细——集成学习——Adaboost实现多分类——附代码
  • 串口通信标准RS232 RS485 RS422的区别
  • jdk环境安装
  • QT+网络调试助手+TCP服务器