Message
Display global messages as feedback in response to user operations.
When To Use
- To provide feedback such as success, warning, error etc.
- A message is displayed at top and center and will be dismissed automatically, as a non-interrupting light-weighted prompt.
Examples
Basic
vue
<template>
<a-button type="primary" @click="info">Display normal message</a-button>
</template>
<script setup>
import { message } from 'ant-design-vue'
const info = () => {
message.info('This is a normal message')
}
</script>1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
Other types of message
vue
<template>
<div>
<a-button @click="success">Success</a-button>
<a-button @click="error">Error</a-button>
<a-button @click="warning">Warning</a-button>
</div>
</template>
<script setup>
import { message } from 'ant-design-vue'
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')
}
</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
Customize duration
vue
<template>
<a-button @click="success">Customized display duration</a-button>
</template>
<script setup>
import { message } from 'ant-design-vue'
const success = () => {
message.success('This is a prompt message for success, and it will disappear in 10 seconds', 10)
}
</script>1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
Loading
vue
<template>
<a-button @click="success">Display a loading indicator</a-button>
</template>
<script setup>
import { message } from 'ant-design-vue'
const success = () => {
const hide = message.loading('Action in progress..', 0)
// Dismiss manually and asynchronously
setTimeout(hide, 2500)
}
</script>1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
Promise interface
vue
<template>
<a-button @click="success">Display sequential messages</a-button>
</template>
<script setup>
import { message } from 'ant-design-vue'
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))
}
</script>1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
Update Message Content
vue
<template>
<a-button type="primary" @click="openMessage">Open the message box</a-button>
</template>
<script setup>
import { message } from 'ant-design-vue'
const openMessage = () => {
const key = 'updatable'
message.loading({ content: 'Loading...', key })
setTimeout(() => {
message.success({ content: 'Loaded!', key, duration: 2 })
}, 1000)
}
</script>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
API
This components provides some static methods, with usage and arguments as following:
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)
| Argument | Description | Type | Default | Version |
|---|---|---|---|---|
| content | The content of the message | string | VNode | function | - | |
| duration | Time(seconds) before auto-dismiss, don't dismiss if set to 0 | number | 3 | |
| onClose | Specify a function that will be called when the message is closed | function | - |
afterClose can be called in thenable interface:
message[level](content, [duration]).then(afterClose)message[level](content, [duration], onClose).then(afterClose)
where level refers one static methods of message. The result of then method will be a Promise.
Supports passing parameters wrapped in an object:
message.open(config)message.success(config)message.error(config)message.info(config)message.warning(config)message.loading(config)
The properties of config are as follows:
| Property | Description | Type | Default | Version |
|---|---|---|---|---|
| content | The content of the message | string | VNode | () => VNode | - | |
| duration | Time(seconds) before auto-dismiss, don't dismiss if set to 0 | number | 3 | |
| getContainer | Return the mount node for Message | () => HTMLElement | () => document.body | |
| icon | Customize message icon | VNode | () => VNode | - | |
| key | The unique identifier of the Message | string | number | - | |
| style | Customized inline style | CSSProperties | - | |
| onClose | Specify a function that will be called when the message is closed | function | - | |
| onClick | Specify a function that will be called when the message is clicked | function | - |
Global static methods
Methods for global configuration and destruction are also provided:
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
| Argument | Description | Type | Default | Version |
|---|---|---|---|---|
| duration | Time before auto-dismiss, in seconds | number | 3 | |
| getContainer | Return the mount node for Message | () => HTMLElement | () => document.body | |
| maxCount | Max message show, drop oldest if exceed limit | number | - | |
| prefixCls | The prefix className of message node | string | ant-message | |
| rtl | Whether to enable RTL mode | boolean | false | |
| top | Distance from top | string | 8px |