Skip to content

Commit 5c9bc51

Browse files
committed
docs(Notification): update docs with FAQ
1 parent a9e133a commit 5c9bc51

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

components/notification/index.en-US.md

+31
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ To display a notification message at any of the four corners of the viewport. Ty
2626
- `notification.open(config)`
2727
- `notification.close(key: String)`
2828
- `notification.destroy()`
29+
- `notification.useNotification()`
2930

3031
The properties of config are as follows:
3132

@@ -74,3 +75,33 @@ notification.config({
7475
| placement | Position of Notification, can be one of `topLeft` `topRight` `bottomLeft` `bottomRight` | string | `topRight` | |
7576
| rtl | Whether to enable RTL mode | boolean | false | |
7677
| top | Distance from the top of the viewport, when `placement` is `topRight` or `topLeft` (unit: pixels). | string | `24px` | |
78+
79+
## FAQ
80+
81+
### Why I can not access context, Pinia, ConfigProvider `locale/prefixCls/theme` in notification?
82+
83+
antdv will dynamic create Vue instance by `Vue.render` when call notification methods. Whose context is different with origin code located context.
84+
85+
When you need context info (like ConfigProvider context), you can use `notification.useNotification` to get `api` instance and `contextHolder` node. And put it in your children:
86+
87+
```html
88+
<template>
89+
<contextHolder />
90+
<!-- <component :is='contextHolder'/> -->
91+
</template>
92+
<script setup>
93+
import { notification } from 'ant-design-vue';
94+
const [notificationApi, contextHolder] = notification.useNotification();
95+
notificationApi.open({
96+
// ...
97+
});
98+
</script>
99+
```
100+
101+
**Note:** You must insert `contextHolder` into your children with hooks. You can use origin method if you do not need context connection.
102+
103+
> [App Package Component](/components/app) can be used to simplify the problem of `useNotification` and other methods that need to manually implant contextHolder.
104+
105+
### How to set static methods prefixCls ?
106+
107+
You can config with [`ConfigProvider.config`](/components/config-provider#configproviderconfig-4130)

components/notification/index.zh-CN.md

+31
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ coverDark: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*9hTIToR-3YYAAA
2727
- `notification.open(config)`
2828
- `notification.close(key: String)`
2929
- `notification.destroy()`
30+
- `notification.useNotification()`
3031

3132
config 参数如下:
3233

@@ -75,3 +76,33 @@ notification.config({
7576
| placement | 弹出位置,可选 `topLeft` `topRight` `bottomLeft` `bottomRight` | string | topRight | |
7677
| rtl | 是否开启 RTL 模式 | boolean | false | 3.0 |
7778
| top | 消息从顶部弹出时,距离顶部的位置,单位像素。 | string | `24px` | |
79+
80+
## FAQ
81+
82+
### 为什么 notification 不能获取 context、Pinia 的内容和 ConfigProvider 的 `locale/prefixCls/theme` 等配置?
83+
84+
直接调用 notification 方法,antdv 会通过 `Vue.render` 动态创建新的 Vue 实体。其 context 与当前代码所在 context 并不相同,因而无法获取 context 信息。
85+
86+
当你需要 context 信息(例如 ConfigProvider 配置的内容)时,可以通过 `notification.useNotification` 方法会返回 `api` 实体以及 `contextHolder` 节点。将其插入到你需要获取 context 位置即可:
87+
88+
```html
89+
<template>
90+
<contextHolder />
91+
<!-- <component :is='contextHolder'/> -->
92+
</template>
93+
<script setup>
94+
import { notification } from 'ant-design-vue';
95+
const [notificationApi, contextHolder] = notification.useNotification();
96+
notificationApi.open({
97+
// ...
98+
});
99+
</script>
100+
```
101+
102+
**异同**:通过 hooks 创建的 `contextHolder` 必须插入到子元素节点中才会生效,当你不需要上下文信息时请直接调用。
103+
104+
> 可通过 [App 包裹组件](/components/app-cn) 简化 `useNotification` 等方法需要手动植入 contextHolder 的问题。
105+
106+
### 静态方法如何设置 prefixCls ?
107+
108+
你可以通过 [`ConfigProvider.config`](/components/config-provider-cn#configproviderconfig-4130) 进行设置。

0 commit comments

Comments
 (0)