Getting Started
This page walks you through the toast module of unix-utils: showing your first toast, handling failures, and using the shortcut APIs.
Import
import {
showToast,
showToastAsync,
hideToast,
showToastSuccess,
showToastError,
showToastInfo
} from '@/uni_modules/unix-utils'The import path is fixed as
@/uni_modules/unix-utils(both the plugin-market import and the npm install + copy land in the project'suni_modules/unix-utils/directory).
Show Your First Toast
// Callback style (mirrors uni.showToast, zero migration cost)
showToast({
title: '保存成功',
icon: 'success',
duration: 2000
})No configuration needed — the channel is selected automatically per platform (App attaches directly to system windows / Web self-drawn DOM / Mini Program native). See Channel Architecture and Fallback for details.
Promise-Style Calls
try {
await showToastAsync({ title: '加载中', icon: 'loading' })
} catch (err) {
// err is a structured ToastFail, not a coarse errMsg
console.error(`toast 失败:errCode=${err.errCode} param=${err.param} platform=${err.platform}`)
}Semantic Shortcut APIs
The three most common toasts, one line each:
showToastSuccess('成功')
showToastError('失败')
showToastInfo('消息')Hide the Toast
showToast({ title: '正在处理…', icon: 'loading', duration: 0 })
// Hide manually once the work is done (reliable hiding on the self-drawn channel, including the position variant)
hideToast()Project-Level Default Configuration (Optional)
When multiple pages need a unified toast style, preset it once with configureToast:
import { configureToast } from '@/uni_modules/unix-utils'
configureToast({
duration: 2000, // default display duration
icon: 'success', // default icon
mask: false // whether to show the mask by default
})Passing null for a field means keeping the library's built-in default (1500ms / 'success' / false). See configureToast() for details.
Next Steps
- How parameters like
icon/positionbehave on each platform: Toast; - When fallbacks happen and how to perceive them: Channel Architecture and Fallback;
- The complete parameter table: ToastOptions.
