---
title: message - 全局提示
nav:
  order: 1
  title: 桌面组件
  path: /design
group:
  order: 6
  title: 反馈组件
  path: /feedback
---

定义：对用户的操作作出轻量的全局反馈。

## 基础用法

最简单的例子。

<code src="./demos/basic.tsx" />

## 不同类型

全局提示有 5 种不同的类型，分别为：info, success, warning, error, normal，loading。

<code src="./demos/types.tsx" />

## 带关闭操作的全局提示

信息较长时，且需用户手动关闭时，可使用。

<code src="./demos/close.tsx" />

## 可自定义延时

自定义时长 10s，默认时长为 3s。

<code src="./demos/time.tsx" />

## API

组件提供了一些静态方法，使用方式和参数如下：

- `message.success(content, [duration], onClose)`
- `message.error(content, [duration], onClose)`
- `message.info(content, [duration], onClose)`
- `message.warning(content, [duration], onClose)`
- `message.warn(content, [duration], onClose)` // alias of warning
- `message.normal(content, [duration], onClose)`
- `message.loading(content, [duration], onClose)`

| 参数     | 说明                                        | 类型                | 默认值 |
| -------- | ------------------------------------------- | ------------------- | ------ |
| content  | 提示内容                                    | ReactNode \| config | -      |
| duration | 自动关闭的延时，单位秒。设为 0 时不自动关闭 | number              | 3      |
| onClose  | 关闭时触发的回调函数                        | function            | -      |

组件同时提供 promise 接口。

- `message[level](content, [duration]).then(afterClose)`
- `message[level](content, [duration], onClose).then(afterClose)`

其中 `message[level]` 是组件已经提供的静态方法。`then` 接口返回值是 Promise。

也可以对象的形式传递参数：

- `message.open(config)`
- `message.success(config)`
- `message.error(config)`
- `message.info(config)`
- `message.warning(config)`
- `message.warn(config)` // alias of warning
- `message.normal(config)`
- `message.loading(config)`

`config` 对象属性如下：

| 参数      | 说明                                        | 类型                                                                                                                                          | 默认值 |
| --------- | ------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- | ------ |
| className | 自定义 CSS class                            | string                                                                                                                                        | -      |
| content   | 提示内容                                    | ReactNode                                                                                                                                     | -      |
| duration  | 自动关闭的延时，单位秒。设为 0 时不自动关闭 | number                                                                                                                                        | 3      |
| icon      | 自定义图标                                  | ReactNode                                                                                                                                     | -      |
| key       | 当前提示的唯一标志                          | string \| number                                                                                                                              | -      |
| style     | 自定义内联样式                              | [CSSProperties](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/e434515761b36830c3e58a970abf5186f005adac/types/react/index.d.ts#L794) | -      |
| onClick   | 点击 message 时触发的回调函数               | function                                                                                                                                      | -      |
| onClose   | 关闭时触发的回调函数                        | function                                                                                                                                      | -      |

### 全局方法

还提供了全局配置和全局销毁方法：

- `message.config(options)`
- `message.destroy()`

> 也可通过 `message.destroy(key)` 来关闭一条消息。

#### message.config

> 当你使用 `ConfigProvider` 进行全局化配置时，系统会默认自动开启 RTL 模式。
>
> 当你想单独使用，可通过如下设置开启 RTL 模式。

```js
message.config({
  top: 100,
  duration: 2,
  maxCount: 3,
  rtl: true,
  prefixCls: 'my-message',
});
```

| 参数         | 说明                                           | 类型              | 默认值              |
| ------------ | ---------------------------------------------- | ----------------- | ------------------- |
| duration     | 默认自动关闭延时，单位秒                       | number            | 3                   |
| getContainer | 配置渲染节点的输出位置                         | () => HTMLElement | () => document.body |
| maxCount     | 最大显示数, 超过限制时，最早的消息会被自动关闭 | number            |
| prefixCls    | 消息节点的 className 前缀                      | string            | `ant-message`       |
| rtl          | 是否开启 RTL 模式                              | boolean           | false               |
| top          | 消息距离顶部的位置                             | number            | 8                   |
