Skip to content

LimeToast 轻提示

轻量级的 toast 组件,用于轻量级反馈或提示,不会打断用户操作,兼容 uniapp/uniappx。

插件依赖:lime-stylelime-sharedlime-overlaylime-icon

文档链接

📚 组件详细文档请访问以下站点:

安装方法

  1. 在 uni-app 插件市场入口 中搜索并导入 lime-toast
  2. 导入后可能需要重新编译项目
  3. 在页面中使用 l-toast 组件(组件)或 lime-toast(演示)

代码演示

基础用法

在页面中放置 l-toast-agent 组件,然后通过 API 调用显示提示。

vue
<template>
  <!-- 在页面放轻提示代理 -->
  <l-toast-agent></l-toast-agent>
</template>
typescript
import { toast, type ToastOptions } from '@/uni_modules/lime-toast'

// 显示普通文本提示
toast({
  message: '这是一条提示信息',
  duration: 2000,
  position: 'middle'
} as ToastOptions)

// 或直接传入文本
toast('这是一条提示信息')

不同类型的提示

支持多种预设类型的提示样式。

typescript
import { toast, success, warning, error, loading, type ToastOptions } from '@/uni_modules/lime-toast'

// 普通文本提示
toast('普通提示')

// 成功提示
success('操作成功')

// 警告提示
warning('警告信息')

// 错误提示
error('操作失败')

// 加载提示(不会自动关闭)
loading('加载中...')

自定义位置

可以设置提示框的显示位置。

typescript
import { toast, type ToastOptions } from '@/uni_modules/lime-toast'

// 顶部显示
toast({
  message: '顶部提示',
  position: 'top'
} as ToastOptions)

// 中间显示
toast({
  message: '中间提示',
  position: 'middle'
} as ToastOptions)

// 底部显示
toast({
  message: '底部提示',
  position: 'bottom'
} as ToastOptions)

自定义图标和方向

可以自定义图标和文字的排列方向。

typescript
import { toast, type ToastOptions } from '@/uni_modules/lime-toast'

// 自定义图标和方向
toast({
  message: '自定义图标',
  icon: 'checkmark-circle',
  direction: 'row', // row 或 column
  iconColor: '#4CAF50',
  iconSize: 24
} as ToastOptions)

手动控制关闭

可以手动关闭提示,或者设置不自动关闭。

typescript
import { loading, hide, hideAll, type ToastOptions } from '@/uni_modules/lime-toast'

// 显示加载提示(不自动关闭)
loading({
  message: '加载中...',
  duration: 0
} as ToastOptions)

// 手动关闭当前提示
setTimeout(() => {
  hide()
}, 3000)

// 或者关闭所有提示
// hideAll()

组件说明

组件名说明
l-toasttoast 基础组件,用于显示单个轻提示内容
l-toast-agenttoast 代理组件,用于在页面中放置,管理 toast 实例的显示与隐藏
lime-toasttoast 演示组件,用于查看示例效果和功能演示

快速预览

导入插件后,可以直接使用以下标签查看演示效果:

html
<!-- 代码位于 uni_modules/lime-toast/components/lime-toast -->
<lime-toast />

Vue2 使用说明

本插件使用了 composition-api,如需在 Vue2 项目中使用,请按照官方教程配置。

关键配置代码(在 main.js 中添加):

js
// vue2
import Vue from 'vue'
import VueCompositionAPI from '@vue/composition-api'
Vue.use(VueCompositionAPI)

API 文档

函数 API

函数名参数说明
toastoptions | message: string | ToastOptions显示普通文本提示,支持直接传入文本或完整配置选项
successmessage: string - 提示内容
options: ToastOptions - 配置选项(可选)
显示成功提示
warningmessage: string - 提示内容
options: ToastOptions - 配置选项(可选)
显示警告提示
errormessage: string - 提示内容
options: ToastOptions - 配置选项(可选)
显示错误提示
loadingmessage: string - 提示内容
options: ToastOptions - 配置选项(可选)
显示加载提示(默认不会自动关闭,需要手动调用 hide())
hideselector: string - 选择器(可选)关闭当前显示的 toast
hideAllselector: string - 选择器(可选)关闭所有显示的 toast

配置项 (ToastOptions)

参数说明类型默认值
message提示内容string''
type提示类型,可选值为 success warning error loadingstring-
duration显示时长(毫秒),0 表示不会自动关闭number2000
position显示位置,可选值为 top middle bottomstring'middle'
direction图标和文字的排列方向,可选值为 row columnstring'column'
overlay是否显示遮罩层booleanfalse
overlayStyle遮罩层样式string | object-
preventScrollThrough是否防止触摸穿透boolean-
zIndex层级number-
icon自定义图标名称string-
iconColor图标颜色string'white'
iconSize图标大小string'20'
iconPrefix图标前缀string-
selector选择器string-
close关闭时的回调函数(未实现)function-
success显示成功后的回调函数(未实现)function-

组件 Props

参数说明类型默认值
direction图标和文字的排列方向,可选值为 row columnstring'row'
duration显示时长(毫秒),0 表示不会自动关闭number2000
position显示位置,可选值为 top middle bottomstring'middle'
overlay是否显示遮罩层booleanfalse
preventScrollThrough是否防止触摸穿透booleantrue
icon自定义图标名称stringundefined
iconPrefix图标前缀stringundefined
iconSize图标大小stringundefined
iconColor图标颜色stringundefined
message提示内容string''
overlayStyle遮罩层样式string | objectundefined
zIndex层级numberundefined
selector选择器stringundefined
type提示类型,可选值为 success warning error loadingstringundefined

主题定制

组件提供了下列 CSS 变量,可用于自定义样式。

名称默认值描述
--l-toast-color#fff文本颜色
--l-toast-z-index12001层级
--l-toast-bg-colorrgba(0, 0, 0, 0.7)背景颜色
--l-toast-max-width700rpx最大宽度
--l-toast-radius$border-radius-lg圆角
--l-toast-line-height1.5行高
--l-toast-row-icon-size24px行布局图标大小
--l-toast-column-icon-size36px列布局图标大小
--l-toast-padding-x$spacer行布局水平内边距
--l-toast-padding-y$spacer-sm行布局垂直内边距
--l-toast-column-radius$border-radius-lg列布局圆角
--l-toast-column-min-width130px列布局最小宽度
--l-toast-column-min-height130px列布局最小高度
--l-toast-column-padding$spacer-md列布局内边距

注意事项

  1. 每个页面只能放置一个 l-toast-agent 组件
  2. 使用 loading 类型时,默认不会自动关闭,需要手动调用 hide()
  3. 在页面卸载时,组件会自动移除事件监听,无需手动处理
  4. 支持同时显示多个 toast 实例,它们会按照显示顺序依次排列
  5. 主题定制时,建议使用 CSS 变量的方式覆盖,避免直接修改组件源码

支持与赞赏

如果你觉得本插件解决了你的问题,可以考虑支持作者:

支付宝赞助微信赞助

源代码

组件源码