Message 全局提示
全局展示操作反馈信息。
何时使用
- 可提供成功、警告和错误等反馈信息。
- 顶部居中显示并自动消失,是一种不打断用户操作的轻量级提示方式。
基本
信息提醒反馈。
vue
<template>
<g-button type="primary" @click="info">Display normal message</g-button>
</template>
<script>
import { message } from '@/components'
export default {
setup() {
const info = () => {
message.info('This is a normal message')
}
return {
info
}
}
}
</script>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
其他提示类型
包括成功、失败、警告。
vue
<template>
<div>
<g-button @click="success">Success</g-button>
<g-button @click="error">Error</g-button>
<g-button @click="warning">Warning</g-button>
</div>
</template>
<script>
import { message } from '@/components'
export default {
setup() {
const success = () => {
message.success('This is a success message')
}
const error = () => {
message.error('This is an error message')
}
const warning = () => {
message.warning('This is a warning message')
}
return {
success,
error,
warning
}
}
}
</script>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
修改延时
自定义时长 10s,默认时长为 3s。
vue
<template>
<g-button @click="success">Customized display duration</g-button>
</template>
<script>
import { message } from '@/components'
export default {
setup() {
const success = () => {
message.success('This is a prompt message for success, and it will disappear in 10 seconds', 10)
}
return {
success
}
}
}
</script>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
加载中
进行全局 loading,异步自行移除。
vue
<template>
<g-button @click="success">Display a loading indicator</g-button>
</template>
<script>
import { message } from '@/components'
export default {
setup() {
const success = () => {
const hide = message.loading('Action in progress..', 0)
// Dismiss manually and asynchronously
setTimeout(hide, 2500)
}
return {
success
}
}
}
</script>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Promise 接口
可以通过 then 接口在关闭后运行 callback 。以上用例将在每个 message 将要结束时通过 then 显示新的 message 。
vue
<template>
<g-button @click="success">Display sequential messages</g-button>
</template>
<script>
import { message } from '@/components'
export default {
setup() {
const success = () => {
message
.loading('Action in progress..', 2.5)
.then(() => message.success('Loading finished', 2.5))
.then(() => message.info('Loading finished is finished', 2.5))
}
return {
success
}
}
}
</script>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
自定义样式
使用 style 和 class 来定义样式。
vue
<template>
<g-button @click="success">Customized style</g-button>
</template>
<script>
import { message } from '@/components'
export default {
setup() {
const success = () => {
message.success({
content: 'This is a prompt message with custom className and style',
className: 'custom-class',
style: {
marginTop: '20vh',
},
})
}
return {
success
}
}
}
</script>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
更新消息内容
可以通过唯一的 key 来更新内容。
vue
<template>
<g-button @click="success">Open message</g-button>
</template>
<script>
import { message } from '@/components'
export default {
setup() {
const key = 'updatable'
const success = () => {
message.loading({ content: 'Loading...', key })
setTimeout(() => {
message.success({ content: 'Loaded!', key, duration: 2 })
}, 1000)
}
return {
success
}
}
}
</script>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
自定义图标
自定义消息图标。
vue
<template>
<g-button @click="success">Customized Icon</g-button>
</template>
<script>
import { message } from '@/components'
import { h } from 'vue'
export default {
setup() {
const success = () => {
message.open({
content: 'This is a message with custom icon',
icon: h('g-icon', { type: 'smile' }),
})
}
return {
success
}
}
}
</script>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
位置
可以自定义位置,只有 top、bottom 两个选项。
vue
<template>
<div>
<g-button @click="info">Display at top</g-button>
<g-divider />
<g-button @click="info2">Display at bottom</g-button>
</div>
</template>
<script>
import { message } from '@/components'
export default {
setup() {
const info = () => {
message.config({
top: '10px',
})
message.info('This message will be displayed at the top')
}
const info2 = () => {
message.config({
bottom: '10px',
})
message.info('This message will be displayed at the bottom')
}
return {
info,
info2
}
}
}
</script>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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 warningmessage.loading(content, [duration], onClose)
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| content | 提示内容 | string | VNode | () => VNode | - |
| 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 warningmessage.loading(config)
config 对象属性如下:
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|---|---|---|---|---|
| class | 自定义 CSS class | string | - | |
| content | 提示内容 | string | VNode | () => VNode | - | |
| duration | 自动关闭的延时,单位秒。设为 0 时不自动关闭。 | number | 3 | |
| icon | 自定义图标 | VNode | () => VNode | - | |
| key | 当前提示的唯一标志 | string | number | - | |
| style | 自定义内联样式 | object | - | |
| onClick | 点击 message 时触发的回调函数 | Function | - | 1.5.0 |
| onClose | 关闭时触发的回调函数 | Function | - |
全局方法
还提供了全局配置和全局销毁方法:
message.config(options)message.destroy()
message.config
js
message.config({
top: '100px',
duration: 2,
maxCount: 3,
rtl: true,
prefixCls: 'my-message',
})1
2
3
4
5
6
7
2
3
4
5
6
7
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|---|---|---|---|---|
| duration | 默认自动关闭延时,单位秒 | number | 3 | |
| getContainer | 配置渲染节点的输出位置 | () => HTMLElement | () => document.body | |
| maxCount | 最大显示数, 超过限制时,最早的消息会被自动关闭 | number | - | |
| prefixCls | 消息节点的 className 前缀 | string | ant-message | 4.5.0 |
| rtl | 是否开启 RTL 模式 | boolean | false | |
| top | 消息距离顶部的位置 | string | 8px |
FAQ
为什么 message 不能获取 context、redux 的内容和 ConfigProvider 的 locale/prefixCls 配置?
直接调用 message 方法,antdv 会通过 Vue.render 动态创建新的 Vue 实体。其 context 与当前代码所在 context 并不相同,因而无法获取 context 信息。
当你需要 context 信息(例如 ConfigProvider 配置的内容)时,可以通过 message.useMessage 方法会返回 api 实体以及 contextHolder 节点。将其插入到你需要获取 context 位置即可:
vue
<template>
<div>
<contextHolder />
<!-- your components -->
</div>
</template>
<script>
import { message } from '@/components'
export default {
setup() {
const [api, contextHolder] = message.useMessage()
const info = () => {
api.info('Hello world!')
}
return {
contextHolder,
info
}
}
}
</script>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
异同:通过 hooks 创建的 contextHolder 必须插入到子元素节点中才会生效,当你不需要上下文信息时请直接调用。
可通过 App 包裹组件 简化
useMessage等方法需要手动植入 contextHolder 的问题。