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

uniapp 封装uni.showToast提示

1、新建toastUtil.js文件,代码如下:

/*** Toast工具类,封装uni.showToast和uni.hideToast* 提供更灵活的Toast显示控制*/
export default {/*** 显示Toast提示* @param {Object} options - Toast配置项* @param {string} options.title - 提示内容* @param {string} [options.icon='none'] - 图标,支持none、success、loading、error* @param {number} [options.duration=2000] - 显示时长,单位ms* @param {string} [options.position='center'] - 位置,支持top、center、bottom* @param {string} [options.mask=false] - 是否显示透明蒙层,防止触摸穿透*/show(options) {// 先隐藏可能存在的Toastthis.hide();// 默认配置const defaultOptions = {title: '',icon: 'none',duration: 2000,position: 'center',mask: false};// 合并配置const toastOptions = { ...defaultOptions, ...options };// 调用uni的showToastuni.showToast(toastOptions);// 如果是加载中图标,不自动关闭(需要手动调用hide)if (toastOptions.icon === 'loading') {// 清除可能存在的定时器if (this.timer) {clearTimeout(this.timer);}} else {// 设置自动关闭定时器this.timer = setTimeout(() => {this.hide();}, toastOptions.duration);}},/*** 隐藏Toast*/hide() {uni.hideToast();// 清除定时器if (this.timer) {clearTimeout(this.timer);this.timer = null;}},/*** 快捷显示成功提示* @param {string} title - 提示内容* @param {number} [duration=2000] - 显示时长*/success(title, duration = 2000) {this.show({title,icon: 'success',duration});},/*** 快捷显示错误提示* @param {string} title - 提示内容* @param {number} [duration=2000] - 显示时长*/error(title, duration = 2000) {this.show({title,icon: 'error',duration});},/*** 快捷显示加载提示* @param {string} [title='加载中'] - 提示内容* @param {boolean} [mask=true] - 是否显示蒙层*/loading(title = '加载中', mask = true) {this.show({title,icon: 'loading',mask});}
};

2、在需要使用的页面引入:

import toastUtil from '@/utils/toastUtil.js';


3、基本使用:

// 显示普通提示
toastUtil.show({title: '操作成功',duration: 3000
});// 显示成功提示
toastUtil.success('提交成功');// 显示错误提示
toastUtil.error('操作失败,请重试');// 显示加载提示(需要手动关闭)
toastUtil.loading('正在加载数据...');// 关闭Toast(特别是用于关闭加载提示)
setTimeout(() => {toastUtil.hide();
}, 2000);

4、这个封装具有以下特点:

  • 提供了统一的调用方式,避免重复配置
  • 支持自动关闭和手动关闭两种模式
  • 针对不同场景提供了快捷方法(success/error/loading)
  • 自动处理定时器,避免内存泄漏
  • 兼容 UniApp 的多端运行环境

文章转载自:

http://6F2dktGC.tgmwy.cn
http://RedUQhkc.tgmwy.cn
http://Dw1x5rWY.tgmwy.cn
http://RlUT04aZ.tgmwy.cn
http://84Vkfbdk.tgmwy.cn
http://ozXAEiBI.tgmwy.cn
http://IUKsEGC0.tgmwy.cn
http://lqkgDZ9Z.tgmwy.cn
http://CSkyRFGi.tgmwy.cn
http://LN4IffPt.tgmwy.cn
http://9KrCIbwf.tgmwy.cn
http://Yk16Dmfh.tgmwy.cn
http://1rVNiwfu.tgmwy.cn
http://tLdSGuGW.tgmwy.cn
http://W4nzTyFm.tgmwy.cn
http://pyxFvYrH.tgmwy.cn
http://KOH1GVV4.tgmwy.cn
http://aqlIdJj8.tgmwy.cn
http://VVez42MT.tgmwy.cn
http://p57GnnqB.tgmwy.cn
http://AXn46dsh.tgmwy.cn
http://oFpUttMr.tgmwy.cn
http://Oj3wE8ML.tgmwy.cn
http://VK5AUTpR.tgmwy.cn
http://DNWkuAZq.tgmwy.cn
http://nKZDrMI5.tgmwy.cn
http://GuSutPPV.tgmwy.cn
http://MqzIiQK1.tgmwy.cn
http://k1NXh9zX.tgmwy.cn
http://dP5C4urz.tgmwy.cn
http://www.dtcms.com/a/368939.html

相关文章:

  • Spring Security 深度学习(六): RESTful API 安全与 JWT
  • 使用CI/CD部署项目(前端Nextjs)
  • Git常用操作(2)
  • LeetCode 刷题【65. 有效数字】
  • Android,jetpack Compose模仿QQ侧边栏
  • 让语言模型自我进化:探索 Self-Refine 的迭代反馈机制
  • Kubernetes(k8s) po 配置持久化挂载(nfs)
  • 支持二次开发的代练App源码:订单管理、代练监控、安全护航功能齐全,一站式解决代练护航平台源码(PHP+ Uni-app)
  • proble1111
  • Ubuntu 24.04.2安装k8s 1.33.4 配置cilium
  • nextcyber——暴力破解
  • Process Explorer 学习笔记(第三章3.2.3):工具栏与参考功能
  • C++两个字符串的结合
  • c51串口通信原理及实操
  • Java垃圾回收算法详解:从原理到实践的完整指南
  • MongoDB 6.0 新特性解读:时间序列集合与加密查询
  • IAR借助在瑞萨RH850/U2A MCU MCAL支持,加速汽车软件开发
  • 状压 dp --- 棋盘覆盖问题
  • 机器学习周报十二
  • 力扣:2322. 从树中删除边的最小分数
  • 人工智能常见分类
  • C++ 音视频开发常见面试题及答案汇总
  • C/C++ Linux系统编程:线程控制详解,从线程创建到线程终止
  • swoole 中 Coroutine\WaitGroup 和channel区别和使用场景
  • HDFS架构核心
  • Python的语音配音软件,使用edge-tts进行文本转语音,支持多种声音选择和语速调节
  • 每周资讯 | 中国游戏市场将在2025年突破500亿美元;《恋与深空》收入突破50亿元
  • 别再手工缝合API了!开源LLMOps神器LMForge,让你像搭积木一样玩转AI智能体!
  • 问卷系统项目自动化测试
  • 事务管理的选择:为何 @Transactional 并非万能,TransactionTemplate 更值得信赖