Skip to content

Message 消息提示

常用于主动操作后的反馈提示。

基础用法

从顶部出现,3 秒后自动消失。

vue
<template>
  <x-button @click="Message.success('成功消息')">成功</x-button>
  <x-button @click="Message.warning('警告消息')">警告</x-button>
  <x-button @click="Message.info('信息消息')">信息</x-button>
  <x-button @click="Message.error('错误消息')">错误</x-button>
</template>

<script setup>
import { Message } from '@xto/feedback'
</script>

可关闭的消息提示

vue
<template>
  <x-button @click="Message({ message: '可关闭的消息', showClose: true })">
    可关闭的消息
  </x-button>
</template>

<script setup>
import { Message } from '@xto/feedback'
</script>

文字居中

vue
<template>
  <x-button @click="Message({ message: '居中的消息', center: true })">居中</x-button>
</template>

<script setup>
import { Message } from '@xto/feedback'
</script>

手动关闭

Message 返回一个实例,可以通过 close 方法手动关闭。

vue
<template>
  <x-button @click="handleClose">手动关闭</x-button>
</template>

<script setup>
import { Message } from '@xto/feedback'

const handleClose = () => {
  const instance = Message({
    message: '这条消息将在3秒后关闭',
    duration: 0 // 不自动关闭
  })

  setTimeout(() => {
    instance.close()
  }, 3000)
}
</script>

全局方法

调用 Message.closeAll() 可以关闭所有消息。

js
import { Message } from '@xto/feedback'

// 关闭所有消息
Message.closeAll()

API

Message 方法

方法名说明参数
Message显示消息提示string | MessageOptions
Message.success显示成功消息string | MessageOptions
Message.warning显示警告消息string | MessageOptions
Message.info显示信息消息string | MessageOptions
Message.error显示错误消息string | MessageOptions
Message.closeAll关闭所有消息

MessageOptions

属性说明类型默认值
message消息文字string
type消息类型'success' | 'warning' | 'info' | 'error''info'
duration显示时间,毫秒。设为 0 则不会自动关闭number3000
showClose是否显示关闭按钮booleanfalse
center文字是否居中booleanfalse
offsetMessage 距离窗口顶部的偏移量number20
customClass自定义类名string
onClose关闭时的回调函数() => void

返回值

Message 方法返回一个 MessageInstance 对象:

属性说明类型
close关闭当前消息() => void

基于 MIT 许可发布