Message 全局提示

全局展示操作反馈信息。

何时使用

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

代码演示

普通提示

信息提醒反馈。

  1. <template>
  2. <button type="button" class="ant-btn ant-btn-primary" @click="openMessage">
  3. <span>显示普通提醒</span>
  4. </button>
  5. </template>
  6. <script>
  7. export default {
  8. methods: {
  9. openMessage() {
  10. this.$message.info("这是一条普通的提醒");
  11. }
  12. }
  13. }
  14. </script>

其他提示类型

包括成功、失败、警告。

  1. <template>
  2. <button type="button" class="ant-btn" @click="openTypeMessage('success')">
  3. <span>success</span>
  4. </button>
  5. <button type="button" class="ant-btn" @click="openTypeMessage('error')">
  6. <span>error</span>
  7. </button>
  8. <button type="button" class="ant-btn" @click="openTypeMessage('warning')">
  9. <span>warning</span>
  10. </button>
  11. </template>
  12. <script>
  13. export default {
  14. methods: {
  15. openTypeMessage(type) {
  16. this.$message[type]("这是一条成功的提醒");
  17. }
  18. }
  19. }
  20. </script>

修改延时

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

  1. <template>
  2. <button type="button" class="ant-btn ant-btn-primary" @click="openMessageCustomizeDuration">
  3. <span>显示延时提醒</span>
  4. </button>
  5. </template>
  6. <script>
  7. export default {
  8. methods: {
  9. openMessageCustomizeDuration() {
  10. this.$message.info("这是一条自定义duration的提醒", 10);
  11. }
  12. }
  13. }
  14. </script>

加载中

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

  1. <template>
  2. <button type="button" class="ant-btn ant-btn-primary" @click="openLoadingMessage">
  3. <span>显示加载中提醒</span>
  4. </button>
  5. </template>
  6. <script>
  7. export default {
  8. methods: {
  9. openLoadingMessage() {
  10. const hide = this.$message.loading("这是一条加载中的提醒", 0);
  11. setTimeout(hide, 2500);
  12. }
  13. }
  14. }
  15. </script>

API

  • this.$message.success(content, duration)
  • this.$message.error(content, duration)
  • this.$message.info(content, duration)
  • this.$message.warning(content, duration)
  • this.$message.loading(content, duration)

Props

组件提供了四个静态方法,参数如下:

参数说明类型默认值
content提示内容string-
duration自动关闭的延时,单位秒number1.5

还提供了一个全局配置方法,在调用前提前配置,全局一次生效。

  • this.$message.config(options)
  • this.$message.destroy()
  1. this.$message.config({
  2. top: 100,
  3. duration: 2,
  4. });
参数说明类型默认值
top消息距离顶部的位置number24px
duration默认自动关闭延时,单位秒number1.5