Skip to content

Toast 轻提示

全局轻提示,通过 useToast() 组合式函数触发,需要在应用内挂载一次 <Toaster /> 作为渲染容器。

基础用法

查看代码
vue
<template>
    <div style="display: flex; gap: 12px; flex-wrap: wrap">
        <Button @click="info('这是一条普通提示')">默认</Button>
        <Button @click="success('操作成功')">成功</Button>
        <Button @click="error('操作失败')">错误</Button>
        <Button @click="warning('请注意')">警告</Button>
        <Toaster />
    </div>
</template>

<script setup lang="ts">
import { Button, Toaster, useToast } from "@a-drowned-fish/rox-v";

const { success, error, warning, info } = useToast();
</script>

使用方式

ts
import { useToast, Toaster } from "@a-drowned-fish/rox-v";

// 1. 在根组件挂载一次渲染容器
<Toaster />;

// 2. 在任意组件调用
const { toast, success, error, warning, info, addToast, removeToast, toasts } = useToast();

toast("普通提示");
success("成功");
error("失败");
warning("警告");
info("信息");

useToast 返回值

名称说明
toast(message, options?)弹出默认(info)提示
success(message, options?)成功提示
error(message, options?)错误提示
warning(message, options?)警告提示
info(message, options?)信息提示
addToast(options)传入完整 ToastOptions
removeToast(id)根据 id 关闭某条提示
toasts当前所有提示的响应式数组

ToastOptions

字段说明类型默认值
message提示文本string""
duration自动关闭时长(ms),0 不自动关闭number2000
type类型"success" | "error" | "info" | "warning""info"
iconVisible是否显示图标booleantrue
itemStyle单条自定义样式StyleValue
itemClass单条自定义类名string

Toaster Props(渲染容器)

参数说明类型默认值
toTeleport 目标string"body"
bg提示背景色string
top / bottom垂直位置string
toasterClass / titleClass / messageClass各级类名string""
toasterStyle / itemStyle自定义样式StyleValue

基于 MIT 协议开源