@neosjs/toast
轻提示组件。通过函数式调用在页面中央弹出带图标的通知,支持四种类型、标题与描述、自动关闭时长、手动关闭,以及 closeAll 批量关闭。
安装
sh
pnpm add @neosjs/toastsh
npm i @neosjs/toastsh
yarn add @neosjs/toast快速上手
默认导出 toast 函数,直接传入选项对象调用。
vue
<script setup lang="ts">
import toast from '@neosjs/toast'
function showToast() {
toast({
type: 'success',
title: '提示标题',
message: '这是一条成功提示',
duration: 3000
})
}
</script>
<template>
<button @click="showToast">
显示 Toast
</button>
</template>四种类型
提供 success / info / warning / error 快捷方法,第一个参数为提示内容。
ts
import toast from '@neosjs/toast'
toast.success('操作成功')
toast.info('这是一条信息提示')
toast.warning('请注意风险')
toast.error('操作失败')关闭与回调
duration设为0时不会自动关闭,需手动关闭。closable开启后显示右上角关闭按钮。- 调用返回实例的
close()可主动关闭单条;toast.closeAll()关闭全部。
ts
const instance = toast.success('操作成功', {
closable: true,
duration: 0,
onClose: () => {
console.log('已关闭')
}
})
// 主动关闭
instance.close()
// 关闭全部
toast.closeAll()TIP
鼠标悬停在提示上会暂停自动关闭倒计时,移开后重新开始。
Options
toast(options) 的参数对象:
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| type | 'info' | 'success' | 'warning' | 'error' | 'success' | 提示类型(决定图标) |
| message | string | — | 提示内容 |
| title | string | '' | 提示标题,为空则不显示 |
| duration | number | 3000 | 自动关闭延时(ms),0 表示不自动关闭 |
| closable | boolean | false | 是否显示关闭按钮 |
| onClose | () => void | — | 关闭时的回调函数 |
| ctx | AppContext | — | 传入应用上下文,使 Toast 内可使用全局注册的组件 |
返回值
每次调用返回当前 Toast 实例:
| 属性 / 方法 | 类型 | 说明 |
|---|---|---|
| id | string | 实例唯一标识 |
| close | () => void | 关闭当前 Toast |