Files
2026-10-13 14:17:50 +08:00

70 lines
2.2 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Message 消息提示
`Message` 是对 TDesign Mobile MessagePlugin 的轻量封装,支持字符串调用、配置对象和统一的主题方法。
## 导入
```ts
import { Message } from "timi-tdesign-mobile";
```
`Message`、`MessageInput` 和 `MessageOptions` 均从库入口导出。使用前需按[快速开始](/guide/)引入 TDesign Mobile 样式。
## 基础用法
```ts
Message("正在加载");
Message.success("保存成功");
Message.warning("请先选择客户");
Message.error("保存失败");
```
直接调用 `Message(input)` 时,主题读取配置对象的 `theme`,未指定时使用 `info`。`Message.info`、`Message.success`、`Message.warning` 和 `Message.error` 会固定对应主题;这些快捷方法默认设置 `offset: [10, 16]`,配置对象中的 `offset` 可以覆盖默认值。
## 配置对象与显示时长
`input` 可以是字符串,也可以是 TDesign Mobile 的消息配置对象:
```ts
Message.success({
content: "资料已保存",
duration: 5000,
closeBtn: true
});
```
也可以用第二个参数覆盖配置对象中的 `duration`:
```ts
Message.error({ content: "请求失败", duration: 3000 }, 6000);
```
`duration` 的单位是毫秒;未设置时使用 TDesign Mobile 的默认值 3000 毫秒。
## 关闭消息
```ts
Message.closeAll();
```
该方法会关闭当前由 TDesign MessagePlugin 创建的全部消息。
## 类型与配置项
```ts
import type { MessageInput, MessageOptions } from "timi-tdesign-mobile";
```
`MessageInput` 为 `string | MessageOptions`。`MessageOptions` 继承 TDesign Mobile 的 `TdMessageProps`,并额外支持 `context?: Element`,可指定消息挂载的 DOM 容器。
常用配置项:
| 配置项 | 类型 | 说明 |
| --- | --- | --- |
| `content` | `string \| TNode` | 消息内容。 |
| `theme` | `"info" \| "success" \| "warning" \| "error"` | 消息主题;直接调用 `Message` 时默认是 `info`。 |
| `duration` | `number` | 显示时长,单位为毫秒,默认 3000。 |
| `offset` | `(string \| number)[]` | 消息位置偏移;主题快捷方法默认是 `[10, 16]`。 |
| `context` | `Element` | 消息挂载的 DOM 容器。 |
其余选项(如 `align`、`closeBtn`、`icon`、`link`、`marquee`、`zIndex`)沿用 TDesign Mobile Message 配置。