Toast 轻提示
介绍
在页面中间弹出黑色半透明提示,用于消息通知、加载提示、操作结果提示等场景。
导入
js
import Toast from '@/components/feedback/Toast.vue';使用方法
基础用法
Toast 组件需要使用 ref 引用,并通过调用 show 方法或其他快捷方法来显示提示信息。
vue
<template>
<Toast ref="toast1" />
<Button @click="showToast">显示提示</Button>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import Toast, { type ToastInstance } from '@/components/feedback/Toast.vue';
import Button from '@/components/basic/Button.vue';
const toast1 = ref<ToastInstance>();
function showToast() {
toast1.value?.info('这是一个提示!');
}
</script>不同类型的提示
Toast 支持多种类型的提示,包括文字提示、成功提示、失败提示、无网络提示等。
vue
<template>
<Toast ref="toast1" />
<FlexRow wrap :padding="10" :gap="10">
<Button @click="toast1?.info('这是一个提示!')">文字提示</Button>
<Button @click="toast1?.success('这是一个成功提示!')">成功提示</Button>
<Button @click="toast1?.fail('这是一个失败提示!')">失败提示</Button>
<Button @click="toast1?.offline('网络不给力,请稍后再试')">无网络提示</Button>
</FlexRow>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import Toast, { type ToastInstance } from '@/components/feedback/Toast.vue';
import Button from '@/components/basic/Button.vue';
import FlexRow from '@/components/layout/FlexRow.vue';
const toast1 = ref<ToastInstance>();
</script>自定义位置
Toast 默认渲染在屏幕正中位置,通过 position 属性可以控制 Toast 展示的位置。
vue
<template>
<Toast ref="toast1" position="center" />
<Toast ref="toast2" position="top" />
<Toast ref="toast3" position="bottom" />
<FlexRow wrap :padding="10" :gap="10">
<Button @click="toast1?.info('在中间显示')">中间显示</Button>
<Button @click="toast2?.info('在顶部显示')">顶部显示</Button>
<Button @click="toast3?.info('在底部显示')">底部显示</Button>
</FlexRow>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import Toast, { type ToastInstance } from '@/components/feedback/Toast.vue';
import Button from '@/components/basic/Button.vue';
import FlexRow from '@/components/layout/FlexRow.vue';
const toast1 = ref<ToastInstance>();
const toast2 = ref<ToastInstance>();
const toast3 = ref<ToastInstance>();
</script>动态修改提示
可以通过 updateProps 方法动态修改 Toast 的内容。
vue
<template>
<Toast ref="toast1" />
<Button @click="showAndUpdate">显示并更新提示</Button>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import Toast, { type ToastInstance } from '@/components/feedback/Toast.vue';
import Button from '@/components/basic/Button.vue';
const toast1 = ref<ToastInstance>();
function showAndUpdate() {
toast1.value?.loading({ content: '加载中请稍后', duration: -1 });
let time = 0;
const timer = setInterval(() => {
if (time < 8) {
time++;
toast1.value?.updateProps({ content: `加载中 (${time}/8)...` });
} else {
toast1.value?.close();
clearInterval(timer);
}
}, 1000);
}
</script>API 参考
Toast
轻提示组件。
Props
| 名称 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| position | 提示显示位置 | 'top' | 'bottom' | 'center' | - | 'center' |
| forbidClick | 是否禁止背景点击 | boolean | - | false |
| closeOnClick | 是否在点击后关闭 | boolean | - | true |
| maskStyle | 土司背景的自定义样式 | ViewStyle | - | - |
| toastStyle | 土司提示容器的自定义样式 | ViewStyle | - | - |
| textStyle | 土司提示文字的自定义样式 | TextStyle | - | - |
| iconProps | 图标自定义属性 | IconProps | - | - |
Slots
| 名称 | 说明 |
|---|---|
| content | 提示内容区域,可以自定义提示的内容 |
Events
| 名称 | 说明 | 参数 |
|---|---|---|
| click | 点击提示区域时触发 | $event |
方法
show(options: ToastShowProps): void
显示提示信息。
参数
| 名称 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| options | 提示信息选项 | ToastShowProps | 是 | - |
info(options?: ToastShowProps|string): void
显示信息提示。
参数
| 名称 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| options | 提示信息选项或内容字符串 | ToastShowProps|string | 否 | - |
success(options?: ToastShowProps|string): void
显示成功提示。
参数
| 名称 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| options | 提示信息选项或内容字符串 | ToastShowProps|string | 否 | - |
fail(options?: ToastShowProps|string): void
显示失败提示。
参数
| 名称 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| options | 提示信息选项或内容字符串 | ToastShowProps|string | 否 | - |
offline(options?: ToastShowProps|string): void
显示网络错误提示。
参数
| 名称 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| options | 提示信息选项或内容字符串 | ToastShowProps|string | 否 | - |
loading(options?: ToastShowProps|string): void
显示加载中提示。
参数
| 名称 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| options | 提示信息选项或内容字符串 | ToastShowProps|string | 否 | - |
text(options?: ToastShowProps|string): void
显示纯文本提示。
参数
| 名称 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| options | 提示信息选项或内容字符串 | ToastShowProps|string | 否 | - |
hide(): void
隐藏提示信息。
close(): void
关闭提示信息(与hide功能相同)。
updateProps(options?: ToastShowProps): void
动态更新提示信息的属性。
参数
| 名称 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| options | 需要更新的提示信息选项 | ToastShowProps | 否 | - |
ToastShowProps
提示信息的选项接口。
| 名称 | 说明 | 类型 | 默认值 | 可选值 |
|---|---|---|---|---|
| duration | 自动关闭的延时,单位ms。0:根据内容长度自动设置关闭时间,最小3000ms,最大15000ms;-1:一直显示,直到手动关闭 | number | 0 | 0, -1, 其他正数 |
| type | 图标类型 | string | text | text, loading, success, fail, info, offline |
| icon | 自定义图标 | string | - | - |
| content | 提示内容 | string | - | - |
| onClose | 关闭后回调函数 | () => void | - | - |
主题变量
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| ToastBackgroundColor | string | background.toast | - |
| ToastTextColor | string | white | - |
| ToastIconSize | number | 85 | - |
| ToastIconMarginBottom | number | 12 | - |
| ToastMarginWhenTop | number | 200 | - |
| ToastMarginWhenBottom | number | 200 | - |
| ToastIconSuccess | string | success | - |
| ToastIconError | string | error | - |
| ToastIconOffline | string | cry | - |
| ToastIconInfo | string | prompt | - |
| ToastPaddingVertical | number | 25 | - |
| ToastPaddingHorizontal | number | 30 | - |
| ToastRadius | number | 16 | - |