全局提示

全局展示操作反馈信息。

何时使用

  • 可提供成功、警告和错误等反馈信息。
  • 顶部居中显示并自动消失,是一种不打断用户操作的轻量级提示方式。

代码演示

Message全局提示 - 图1

普通提示

信息提醒反馈。

  1. <template>
  2. <a-button type="primary" @click="info">Display normal message</a-button>
  3. </template>
  4. <script>
  5. export default {
  6. methods: {
  7. info () {
  8. this.$message.info('This is a normal message');
  9. },
  10. }
  11. }
  12. </script>

Message全局提示 - 图2

修改延时

自定义时长 10s,默认时长为 3s

  1. <template>
  2. <a-button @click="success">Customized display duration</a-button>
  3. </template>
  4. <script>
  5. export default {
  6. methods: {
  7. success () {
  8. this.$message.success('This is a prompt message for success, and it will disappear in 10 seconds', 10);
  9. },
  10. }
  11. }
  12. </script>

Message全局提示 - 图3

加载中

进行全局 loading,异步自行移除。

  1. <template>
  2. <a-button @click="success">Display a loading indicator</a-button>
  3. </template>
  4. <script>
  5. export default {
  6. methods: {
  7. success () {
  8. const hide = this.$message.loading('Action in progress..', 0);
  9. setTimeout(hide, 2500);
  10. },
  11. }
  12. }
  13. </script>

Message全局提示 - 图4

其他提示类型

包括成功、失败、警告。

  1. <template>
  2. <div>
  3. <a-button @click="success">Success</a-button>
  4. <a-button @click="error">Error</a-button>
  5. <a-button @click="warning">Warning</a-button>
  6. </div>
  7. </template>
  8. <script>
  9. export default {
  10. methods: {
  11. success () {
  12. this.$message.success('This is a message of success');
  13. },
  14. error () {
  15. this.$message.error('This is a message of error');
  16. },
  17. warning () {
  18. this.$message.warning('This is message of warning');
  19. },
  20. }
  21. }
  22. </script>

Message全局提示 - 图5

Promise 接口

可以通过 then 接口在关闭后运行 callback 。以上用例将在每个 message 将要结束时通过 then 显示新的 message 。

  1. <template>
  2. <a-button @click="success">Display a sequence of message</a-button>
  3. </template>
  4. <script>
  5. export default {
  6. methods: {
  7. success () {
  8. this.$message.loading('Action in progress..', 2.5)
  9. .then(() => this.$message.success('Loading finished', 2.5))
  10. .then(() => this.$message.info('Loading finished is finished', 2.5));
  11. },
  12. }
  13. }
  14. </script>

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 warning
  • message.loading(content, [duration], onClose)
    参数说明类型默认值
    content提示内容string| VNode |(h) => VNode-
    duration自动关闭的延时,单位秒。设为 0 时不自动关闭。number3
    onClose关闭时触发的回调函数Function-

组件同时提供 promise 接口

  • messagelevel.then(afterClose)
  • messagelevel.then(afterClose)
    其中message[level] 是组件已经提供的静态方法。then 接口返回值是 Promise 。

  • message.open(config)

    参数说明类型默认值
    content提示内容string| VNode |(h) => VNode-
    duration自动关闭的延时,单位秒。设为 0 时不自动关闭。number3
    onClose关闭时触发的回调函数Function-
    icon自定义图标string| VNode |(h) => VNode-

全局方法

还提供了全局配置和全局销毁方法:

  • message.config(options)
  • message.destroy()

message.config

  1. message.config({
  2. top: `100px`,
  3. duration: 2,
  4. maxCount: 3,
  5. });
参数说明类型默认值
duration默认自动关闭延时,单位秒number3
getContainer配置渲染节点的输出位置() => HTMLElement() => document.body
maxCount最大显示数, 超过限制时,最早的消息会被自动关闭number-
top消息距离顶部的位置string24px