Skip to content

Commit 79da2d1

Browse files
committed
docs(Message): update docs with FAQ
1 parent 11a5b54 commit 79da2d1

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

components/message/index.en-US.md

+31
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Methods for global configuration and destruction are also provided:
6464

6565
- `message.config(options)`
6666
- `message.destroy()`
67+
- `message.useMessage()`
6768

6869
#### message.config
6970

@@ -85,3 +86,33 @@ message.config({
8586
| prefixCls | The prefix className of message node | string | `ant-message` | 3.0 |
8687
| rtl | Whether to enable RTL mode | boolean | false | 3.0 |
8788
| top | distance from top | string | `8px` | |
89+
90+
## FAQ
91+
92+
### Why I can not access context, Pinia, ConfigProvider `locale/prefixCls/theme` in message?
93+
94+
antdv will dynamic create Vue instance by `Vue.render` when call message methods. Whose context is different with origin code located context.
95+
96+
When you need context info (like ConfigProvider context), you can use `message.useMessage` to get `api` instance and `contextHolder` node. And put it in your children:
97+
98+
```html
99+
<template>
100+
<contextHolder />
101+
<!-- <component :is='contextHolder'/> -->
102+
</template>
103+
<script setup>
104+
import { message } from 'ant-design-vue';
105+
const [messageApi, contextHolder] = message.useMessage();
106+
messageApi.open({
107+
// ...
108+
});
109+
</script>
110+
```
111+
112+
**Note:** You must insert `contextHolder` into your children with hooks. You can use origin method if you do not need context connection.
113+
114+
> [App Package Component](/components/app) can be used to simplify the problem of `useMessage` and other methods that need to manually implant contextHolder.
115+
116+
### How to set static methods prefixCls ?
117+
118+
You can config with [`ConfigProvider.config`](/components/config-provider#configproviderconfig-4130)

components/message/index.zh-CN.md

+31
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ coverDark: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*7qMTRoq3ZGkAAA
6767

6868
- `message.config(options)`
6969
- `message.destroy()`
70+
- `message.useMessage()`
7071

7172
#### message.config
7273

@@ -88,3 +89,33 @@ message.config({
8889
| prefixCls | 消息节点的 className 前缀 | string | `ant-message` | 3.0 | |
8990
| rtl | 是否开启 RTL 模式 | boolean | false | | |
9091
| top | 消息距离顶部的位置 | string | `8px` | | |
92+
93+
## FAQ
94+
95+
### 为什么 message 不能获取 context、Pinia 的内容和 ConfigProvider 的 `locale/prefixCls/theme` 等配置?
96+
97+
直接调用 message 方法,antdv 会通过 `Vue.render` 动态创建新的 Vue 实体。其 context 与当前代码所在 context 并不相同,因而无法获取 context 信息。
98+
99+
当你需要 context 信息(例如 ConfigProvider 配置的内容)时,可以通过 `message.useMessage` 方法会返回 `api` 实体以及 `contextHolder` 节点。将其插入到你需要获取 context 位置即可:
100+
101+
```html
102+
<template>
103+
<contextHolder />
104+
<!-- <component :is='contextHolder'/> -->
105+
</template>
106+
<script setup>
107+
import { message } from 'ant-design-vue';
108+
const [messageApi, contextHolder] = message.useMessage();
109+
messageApi.open({
110+
// ...
111+
});
112+
</script>
113+
```
114+
115+
**异同**:通过 hooks 创建的 `contextHolder` 必须插入到子元素节点中才会生效,当你不需要上下文信息时请直接调用。
116+
117+
> 可通过 [App 包裹组件](/components/app-cn) 简化 `useMessage` 等方法需要手动植入 contextHolder 的问题。
118+
119+
### 静态方法如何设置 prefixCls ?
120+
121+
你可以通过 [`ConfigProvider.config`](/components/config-provider-cn#configproviderconfig-4130) 进行设置。

0 commit comments

Comments
 (0)