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) {
    // 先隐藏可能存在的Toast
    this.hide();
    
    // 默认配置
    const defaultOptions = {
      title: '',
      icon: 'none',
      duration: 2000,
      position: 'center',
      mask: false
    };
    
    // 合并配置
    const toastOptions = { ...defaultOptions, ...options };
    
    // 调用uni的showToast
    uni.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 的多端运行环境
相关推荐
审小匠OpenCPAi几秒前
银行流水核查怎么自动化?单边匹配、双向勾稽与图聚类异常检测的工程对比
java·前端·人工智能·python·审计
Revolution614 分钟前
一个公共表格组件,是怎么一步步失控的
前端·前端工程化
mONESY7 分钟前
React useState 核心原理与最佳实践:从异步更新到性能优化全解
javascript
腻害兔20 分钟前
【若依项目-产品经理视角】RuoYi-Vue-Pro 源码拆解:CRM 客户关系模块深度解析——从线索到回款,一套完整的 B2B 销售闭环是怎么搭的?
java·前端·javascript·vue.js·产品经理·ai编程
whyfail28 分钟前
前端学 Spring Boot(2):一次点击,如何穿过整个后端?
前端·spring boot·后端
AI多Agent协作实战派1 小时前
AI多Agent协作系统实战(二十三):Agent读HEARTBEAT.md不读AGENTS.md——openclaw的文件加载之谜
前端·数据库·人工智能·uni-app
gis开发之家1 小时前
《Vue3 从入门到大神37篇》Vue3 源码详解(七):watch 与 watchEffect 源码对比——副作用是如何被追踪的?
javascript·前端框架·vue3·vue3源码
2601_955760072 小时前
如何用 Claude Opus 5 API 批量扩展长尾关键词和文章选题
前端·python·搜索引擎
KaMeidebaby2 小时前
卡梅德生物技术快报|bli亲和力检测gst:告别批量跑胶:BLI实时酶切监测技术加速GST融合蛋白下游流程优化
前端·网络·数据库·人工智能·算法
bloglin999992 小时前
langchain 和 langgraph 和 react
javascript·react.js·langchain