2025-04-01 15:10:18 +08:00
|
|
|
---
|
|
|
|
|
outline: deep
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# Vben Alert 轻量提示框
|
|
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
`Alert` 提供了一组纯 JavaScript 调用的轻量提示框能力,适合快速创建 `alert`、`confirm`、`prompt` 这类简单交互。
|
2025-04-01 15:10:18 +08:00
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
::: info 适用场景
|
2025-04-01 15:10:18 +08:00
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
`Alert` 与 `Modal` 的能力有部分重叠,但更适合临时确认、简单提示和轻量输入场景。复杂弹窗仍然建议使用 `Vben Modal`。:::
|
2025-04-01 15:10:18 +08:00
|
|
|
|
2025-04-15 00:00:05 +08:00
|
|
|
::: tip 注意
|
|
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
通过 `alert`、`confirm`、`prompt` 动态创建的弹窗,在已经打开的情况下不支持 HMR 热更新。修改相关代码后,需要关闭后重新打开。:::
|
2025-04-01 15:10:18 +08:00
|
|
|
|
|
|
|
|
## 基础用法
|
|
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
使用 `alert` 创建只有确认按钮的提示框。
|
2025-04-01 15:10:18 +08:00
|
|
|
|
|
|
|
|
<DemoPreview dir="demos/vben-alert/alert" />
|
|
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
使用 `confirm` 创建带确认和取消按钮的提示框。
|
2025-04-01 15:10:18 +08:00
|
|
|
|
|
|
|
|
<DemoPreview dir="demos/vben-alert/confirm" />
|
|
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
使用 `prompt` 创建可接收用户输入的提示框。
|
2025-04-01 15:10:18 +08:00
|
|
|
|
|
|
|
|
<DemoPreview dir="demos/vben-alert/prompt" />
|
|
|
|
|
|
2025-04-15 00:00:05 +08:00
|
|
|
## useAlertContext
|
|
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
当 `content`、`footer` 或 `icon` 使用的是自定义组件时,可以在这些组件内部通过 `useAlertContext()` 获取当前弹窗上下文,并主动触发确认或取消。
|
2025-04-15 00:00:05 +08:00
|
|
|
|
2025-04-15 20:52:23 +08:00
|
|
|
::: tip 注意
|
|
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
`useAlertContext` 只能在 `setup` 或函数式组件中使用。:::
|
2025-04-15 00:00:05 +08:00
|
|
|
|
|
|
|
|
### Methods
|
|
|
|
|
|
2026-03-14 21:34:48 +08:00
|
|
|
| 方法 | 描述 | 类型 | 版本要求 |
|
|
|
|
|
| --------- | ---------------------- | ------------ | -------- |
|
|
|
|
|
| doConfirm | 触发当前弹窗的确认操作 | `() => void` | `>5.5.4` |
|
|
|
|
|
| doCancel | 触发当前弹窗的取消操作 | `() => void` | `>5.5.4` |
|
2025-04-15 00:00:05 +08:00
|
|
|
|
2025-04-01 15:10:18 +08:00
|
|
|
## 类型说明
|
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
export type IconType = 'error' | 'info' | 'question' | 'success' | 'warning';
|
|
|
|
|
|
2025-04-01 22:55:29 +08:00
|
|
|
export type BeforeCloseScope = {
|
|
|
|
|
isConfirm: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
2025-04-01 15:10:18 +08:00
|
|
|
export type AlertProps = {
|
2025-04-01 22:55:29 +08:00
|
|
|
beforeClose?: (
|
|
|
|
|
scope: BeforeCloseScope,
|
|
|
|
|
) => boolean | Promise<boolean | undefined> | undefined;
|
2025-04-01 15:10:18 +08:00
|
|
|
bordered?: boolean;
|
2025-04-07 01:21:30 +08:00
|
|
|
buttonAlign?: 'center' | 'end' | 'start';
|
2025-04-01 15:10:18 +08:00
|
|
|
cancelText?: string;
|
|
|
|
|
centered?: boolean;
|
|
|
|
|
confirmText?: string;
|
|
|
|
|
containerClass?: string;
|
|
|
|
|
content: Component | string;
|
|
|
|
|
contentClass?: string;
|
2025-04-07 01:21:30 +08:00
|
|
|
contentMasking?: boolean;
|
2025-04-14 11:48:21 +08:00
|
|
|
footer?: Component | string;
|
2025-04-01 15:10:18 +08:00
|
|
|
icon?: Component | IconType;
|
2025-04-14 11:48:21 +08:00
|
|
|
overlayBlur?: number;
|
2025-04-01 15:10:18 +08:00
|
|
|
showCancel?: boolean;
|
|
|
|
|
title?: string;
|
|
|
|
|
};
|
|
|
|
|
|
2025-04-07 01:21:30 +08:00
|
|
|
export type PromptProps<T = any> = {
|
|
|
|
|
beforeClose?: (scope: {
|
|
|
|
|
isConfirm: boolean;
|
|
|
|
|
value: T | undefined;
|
|
|
|
|
}) => boolean | Promise<boolean | undefined> | undefined;
|
|
|
|
|
component?: Component;
|
|
|
|
|
componentProps?: Recordable<any>;
|
2026-03-14 21:34:48 +08:00
|
|
|
componentSlots?:
|
|
|
|
|
| (() => any)
|
|
|
|
|
| Recordable<unknown>
|
|
|
|
|
| VNode
|
|
|
|
|
| VNodeArrayChildren;
|
2025-04-07 01:21:30 +08:00
|
|
|
defaultValue?: T;
|
|
|
|
|
modelPropName?: string;
|
|
|
|
|
} & Omit<AlertProps, 'beforeClose'>;
|
|
|
|
|
|
2025-04-01 15:10:18 +08:00
|
|
|
export function alert(options: AlertProps): Promise<void>;
|
|
|
|
|
export function alert(
|
|
|
|
|
message: string,
|
|
|
|
|
options?: Partial<AlertProps>,
|
|
|
|
|
): Promise<void>;
|
|
|
|
|
export function alert(
|
|
|
|
|
message: string,
|
|
|
|
|
title?: string,
|
|
|
|
|
options?: Partial<AlertProps>,
|
|
|
|
|
): Promise<void>;
|
|
|
|
|
|
|
|
|
|
export async function prompt<T = any>(
|
|
|
|
|
options: Omit<AlertProps, 'beforeClose'> & {
|
|
|
|
|
beforeClose?: (
|
2025-04-01 22:55:29 +08:00
|
|
|
scope: BeforeCloseScope & {
|
|
|
|
|
value: T;
|
|
|
|
|
},
|
2025-04-01 15:10:18 +08:00
|
|
|
) => boolean | Promise<boolean | undefined> | undefined;
|
|
|
|
|
component?: Component;
|
|
|
|
|
componentProps?: Recordable<any>;
|
|
|
|
|
defaultValue?: T;
|
|
|
|
|
modelPropName?: string;
|
|
|
|
|
},
|
|
|
|
|
): Promise<T | undefined>;
|
|
|
|
|
```
|