Skip to content

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>

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>

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>

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>

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>

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>

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 warning
  • message.loading(content, [duration], onClose)
ArgumentDescriptionTypeDefaultVersion
contentThe content of the messagestring | VNode | function-
durationTime(seconds) before auto-dismiss, don't dismiss if set to 0number3
onCloseSpecify a function that will be called when the message is closedfunction-

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:

PropertyDescriptionTypeDefaultVersion
contentThe content of the messagestring | VNode | () => VNode-
durationTime(seconds) before auto-dismiss, don't dismiss if set to 0number3
getContainerReturn the mount node for Message() => HTMLElement() => document.body
iconCustomize message iconVNode | () => VNode-
keyThe unique identifier of the Messagestring | number-
styleCustomized inline styleCSSProperties-
onCloseSpecify a function that will be called when the message is closedfunction-
onClickSpecify a function that will be called when the message is clickedfunction-

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',
})
ArgumentDescriptionTypeDefaultVersion
durationTime before auto-dismiss, in secondsnumber3
getContainerReturn the mount node for Message() => HTMLElement() => document.body
maxCountMax message show, drop oldest if exceed limitnumber-
prefixClsThe prefix className of message nodestringant-message
rtlWhether to enable RTL modebooleanfalse
topDistance from topstring8px

Released under the MIT License.