Skip to content

全局提示 Message

全局展示操作反馈信息

何时使用

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

使用

  • message.value.open(content: string | Message) // open 调用
  • message.value.info(content: string | Message) // info 调用
  • message.value.success(content: string | Message) // success 调用
  • message.value.error(content: string | Message) // error 调用
  • message.value.warning(content: string | Message) // warning 调用
  • message.value.loading(content: string | Message) // loading 调用

基本使用

Open
Show Code
vue
<script setup lang="ts">
import { ref } from 'vue'
const message = ref()
function onOpen(content: string) {
  message.value.open(content) // open 调用
}
function onClick(e: Event) {
  console.log('click', e)
}
function onClose() {
  console.log('close')
}
</script>
<template>
  <Button type="primary" @click="onOpen('This is a noraml message')">Open</Button>
  <Message ref="message" @click="onClick" @close="onClose" />
</template>

不同类型的全局提示

Info
Success
Error
Warning
Loading
Show Code
vue
<script setup lang="ts">
import { ref } from 'vue'
const message = ref()
function onInfo(content: string) {
  message.value.info(content) // info 调用
}
function onSuccess(content: string) {
  message.value.success(content) // success 调用
}
function onError(content: string) {
  message.value.error(content) // error 调用
}
function onWarning(content: string) {
  message.value.warning(content) // warning 调用
}
function onLoading(content: string) {
  message.value.loading(content) // loading 调用
}
function onClose() {
  console.log('close')
}
</script>
<template>
  <Space>
    <Button type="primary" @click="onInfo('This is an info message')">Info</Button>
    <Button type="primary" @click="onSuccess('This is a success message')">Success</Button>
    <Button type="primary" @click="onWarning('This is a warning message')">Warning</Button>
    <Button type="primary" @click="onLoading('This is a loading message')">Loading</Button>
  </Space>
  <Message ref="message" @close="onClose" />
</template>

自定义图标

Custom Info Icon
Custom Icon
Show Code
vue
<script setup lang="ts">
import { ref, h } from 'vue'
import { SoundFilled, FireFilled } from '@ant-design/icons-vue'
const message = ref()
function onInfoCustom(content: string) {
  // info 调用, 并自定义图标
  message.value.info({
    content,
    icon: h(SoundFilled)
  })
}
function onOpenCustom(content: string) {
  // open 调用, 并自定义图标
  message.value.open({
    content,
    icon: h(FireFilled, { style: 'color: gold' })
  })
}
function onClose() {
  console.log('close')
}
</script>
<template>
  <Space>
    <Button type="primary" @click="onInfoCustom('This is a custom icon message')">Custom Info Icon</Button>
    <Button type="primary" @click="onOpenCustom('This is a custom icon message')">Custom Icon</Button>
  </Space>
  <Message ref="message" @close="onClose" />
</template>

自定义样式

Custom Class
Custom Style
Show Code
vue
<script setup lang="ts">
import { ref, h } from 'vue'
import { SoundFilled, FireFilled } from '@ant-design/icons-vue'
const message = ref()
function onClassCustom(content: string) {
  message.value.info({
    content,
    icon: h(SoundFilled),
    class: 'custom-class'
  })
}
function onStyleCustom(content: string) {
  message.value.warning({
    content,
    icon: h(FireFilled),
    duration: null,
    top: '30vh',
    style: {
      color: '#f50'
    }
  })
}
function onClose() {
  console.log('close')
}
</script>
<template>
  <Space>
    <Button type="primary" @click="onClassCustom('This is a custom class message')">Custom Class</Button>
    <Button type="primary" @click="onStyleCustom('This is a custom style message')">Custom Style</Button>
  </Space>
  <Message ref="message" @close="onClose" />
</template>
<style lang="less" scoped>
:deep(.custom-class) {
  color: #ff6900;
}
</style>

自定义关闭延时

Custom Close
Never Auto Close
Show Code
vue
<script setup lang="ts">
import { ref } from 'vue'
const message = ref()
function onCustomClose() {
  message.value.info({
    content: 'The message will automatically turn off after 3 seconds.',
    duration: 3000,
    onClose: () => {
      console.log('custom message closed')
    }
  })
}
function onNeverAutoClose() {
  message.value.info({
    content: 'This message will not automatically turn off.',
    duration: null,
    onClick: () => {
      console.log('custom message clicked')
    }
  })
}
function onClose() {
  console.log('close')
}
</script>
<template>
  <Space>
    <Button type="primary" @click="onCustomClose">Custom Close</Button>
    <Button type="primary" @click="onNeverAutoClose">Never Auto Close</Button>
  </Space>
  <Message ref="message" @close="onClose" />
</template>

APIs

Message

参数说明类型默认值
content提示内容stringundefined
duration自动关闭的延时,单位 ms,设置 null 时,不自动关闭number3000
top消息距离顶部的位置,单位 pxstring | number30

Message Type


调用时传入的 Message 类型,以下属性均具有更高优先级

名称说明类型默认值
content?提示内容stringundefined
icon?自定义图标VNodeundefined
duration?自动关闭的延时时长,单位 ms;设置 null 时,不自动关闭number | nullundefined
top?消息距离顶部的位置,单位 pxstring | numberundefined
class?自定义类名stringundefined
style?自定义样式stringundefined
onClick?点击 message 时的回调函数Functionundefined
onClose?关闭时的回调函数Functionundefined

Methods

名称说明类型
open基本全局提示(content: string | Message) => void
info信息全局提示(content: string | Message) => void
success成功全局提示(content: string | Message) => void
error失败全局提示(content: string | Message) => void
warning警告全局提示(content: string | Message) => void
loading加载全局提示(content: string | Message) => void

Events

名称说明类型
click点击 message 时触发的回调函数(e: Event) => void
close关闭时触发的回调函数() => void

全局挂载使用

  • 全局挂载

App.vue

vue
<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount } from 'vue'
const message = ref()
onMounted(() => {
  window['$message'] = message.value
})
onBeforeUnmount(() => {
  delete window['$message']
})
</script>
<template>
  <RouterView />
  <Message ref="message" />
</template>
  • 使用

XXX.vue

vue
<script setup lang="ts">
const $message = window['$message']
function onClick() {
  $message.success('点击了按钮')
}
</script>
<template>
  <Button @click="onClick">按钮</Button>
</template>

Released under the MIT License.