uniapp踩坑 uni.showToast 和 uni.showLoading

embedded/2024/9/24 7:25:16/

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.ppmy.cn/embedded/37282.html

相关文章

git 更换远程仓库地址三种方法总结

git 更换远程仓库地址三种方法总结 一、前言 由于私服的 gitlab 的地址变更&#xff0c;导致部分项目代码提交不上去&#xff0c;需要修改远端仓地址。 其它需要修改远程仓地址的情况如&#xff1a;切换git clone 协议由ssh变为https。 二、环境 windows 10git version 2.3…

LY/T 3131-2019 木质拼花地板检测

木质拼花地板是指通过单元设计&#xff0c;组拼成具有特定图案的木质地板&#xff0c;按照材料组分分为实木拼花地板&#xff0c;实木复合拼花地板和浸渍纸层压拼花地板。 LY/T 3131-2019 实木拼花地板测试项目 测试项目 测试标准 含水率 GB/T 15036.2 漆膜附着力 GB/T 1…

花的花语和传说

花的花语和传说往往紧密相连&#xff0c;它们共同构成了花卉文化的丰富内涵。以下是一些常见花卉的花语和传说&#xff1a; 玫瑰&#xff1a; 花语&#xff1a;爱情、美丽、和平、友谊、勇敢、献身。传说&#xff1a;古希腊和古罗马神话中&#xff0c;玫瑰与美神阿芙洛狄忒&…

关于路由懒加载的实现

在Vue2中&#xff0c;实现路由懒加载可以使用import的动态引入方式。通常&#xff0c;我们可以将组件作为被引入的模块&#xff0c;并在routes配置中使用component: () > import(/components/Example.vue)来实现懒加载。 在Vue3中&#xff0c;懒加载的实现方式稍有不同。Vu…

25_Scala集合Tuple

文章目录 tuple1.元组定义2.Tuple元素访问3.如果元素的len2&#xff0c;称之为键值对对象&#xff0c;也称之为对偶元组4.补充上节Map5.Map集合遍历6.集合之间相互转化 tuple 概念&#xff1a;scala语言采用特殊的方式将无关的数据作为一个整体&#xff0c;组合在一起’ 1.元…

插入排序(Insertion Sort)

插入排序&#xff08;Insertion Sort&#xff09;是一种简单直观的排序算法&#xff0c;它的工作原理如下&#xff1a; 将数组分为已排序部分和未排序部分&#xff1a;初始时&#xff0c;已排序部分仅包含数组的第一个元素&#xff0c;其余元素被视为未排序部分。 从未排序部分…

Android OpenMAX(三)高通OMX组件实现基础

上一节了解了OMX组件实现的基础内容,这一节我们以高通OMX实现为例,简单看看如何实现一个OMX组件。本节代码参考自: omx_core_cmp.cpp qc_omx_component.h omx_vdec.h omx_vdec.cpp Tips:本篇文章旨在简单了解如何实现一个OMX组件,细节的内容不会仔细解读,代码阅读跳跃幅度…

pycharm运行正常,但命令行执行提示module不存在的多种解决方式

在PyCharm设置了Sources Root&#xff0c;向系统变量增加了当前目录的根目录&#xff0c;所以PyCharm运行时能找到自定义包的。但Pyhton命令行执行时少了添加根目录路径的步骤&#xff0c;导致找不到包了。 os.path.dirname获取目录&#xff0c;此处就是获取目录的父目录。如果…