Skip to content

Message 消息提示

常用于主动操作后的反馈提示。与 Notification 的区别是后者更多用于系统级通知的被动提醒。

ts
import { Message } from '@szhn/dh-design-pc'

基础用法

默认情况下在顶部显示,并在 3 秒后自动消失。

<template>
  <el-button @click="show">显示消息</el-button>
</template>

<script lang="ts" setup>
import { Message, Button as ElButton } from '@szhn/dh-design-pc'


function show() {
  Message('这是一条消息提示')
}
</script>

不同状态

用来显示「成功、警告、消息、错误」类的操作反馈。通过 type 字段指定消息类型,也可直接调用对应的快捷方法。

<template>
  <div class="demo-row">
    <el-button @click="Message.success('恭喜你,这是一条成功消息')">Success</el-button>
    <el-button @click="Message.warning('警告,这是一条警告消息')">Warning</el-button>
    <el-button @click="Message.info('这是一条消息')">Info</el-button>
    <el-button @click="Message.error('错误,请稍后再试')">Error</el-button>
    <el-button @click="Message({ message: '这是主要消息', type: 'primary' })">Primary</el-button>
  </div>
</template>

<script lang="ts" setup>
import { Message, Button as ElButton } from '@szhn/dh-design-pc'

</script>

<style scoped>
.demo-row {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}
</style>

Plain 背景

设置 plaintrue 可以得到纯色背景的消息样式。

<template>
  <div class="demo-row">
    <el-button @click="Message({ message: '纯色 Success', type: 'success', plain: true })">Success</el-button>
    <el-button @click="Message({ message: '纯色 Warning', type: 'warning', plain: true })">Warning</el-button>
    <el-button @click="Message({ message: '纯色 Info', type: 'info', plain: true })">Info</el-button>
    <el-button @click="Message({ message: '纯色 Error', type: 'error', plain: true })">Error</el-button>
  </div>
</template>

<script lang="ts" setup>
import { Message, Button as ElButton } from '@szhn/dh-design-pc'

</script>

<style scoped>
.demo-row {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}
</style>

可关闭的消息提示

默认的 Message 是不可以被手动关闭的,通过 showClose 开启关闭按钮,并可通过 duration 控制展示时长,duration0 时消息不会自动关闭。

<template>
  <div class="demo-row">
    <el-button @click="openClosable">可关闭消息</el-button>
    <el-button @click="openStay">不自动关闭</el-button>
  </div>
</template>

<script lang="ts" setup>
import { Message, Button as ElButton } from '@szhn/dh-design-pc'


function openClosable() {
  Message({ message: '点击右侧按钮可以关闭消息', showClose: true, duration: 5000 })
}

function openStay() {
  Message({ message: '我会一直停留,直到手动关闭', showClose: true, duration: 0 })
}
</script>

<style scoped>
.demo-row {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}
</style>

使用 HTML 片段作为正文内容

dangerouslyUseHTMLString 属性设置为 truemessage 就会被当作 HTML 片段处理。请确保内容可信,避免 XSS 攻击。

<template>
  <el-button @click="openHTML">展示富文本消息</el-button>
</template>

<script lang="ts" setup>
import { Message, Button as ElButton } from '@szhn/dh-design-pc'


function openHTML() {
  Message({
    dangerouslyUseHTMLString: true,
    message: '<strong>这是 <i style="color: var(--el-color-primary);">HTML</i> 片段</strong>',
  })
}
</script>

分组消息合并

设置 groupingtrue,内容相同的消息将以徽标形式合并展示。

<template>
  <div class="demo-row">
    <el-button @click="openGroup">合并消息</el-button>
    <el-button @click="openNormal">不合并</el-button>
  </div>
</template>

<script lang="ts" setup>
import { Message, Button as ElButton } from '@szhn/dh-design-pc'


function openGroup() {
  Message({ message: '相同内容会被合并', grouping: true, type: 'success' })
}

function openNormal() {
  Message({ message: '独立展示的消息', type: 'info' })
}
</script>

<style scoped>
.demo-row {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}
</style>

消息位置

通过 placement 控制消息的出现位置,支持 top / bottom 等。

<template>
  <div class="demo-row">
    <el-button @click="openTop">顶部显示</el-button>
    <el-button @click="openBottom">底部显示</el-button>
  </div>
</template>

<script lang="ts" setup>
import { Message, Button as ElButton } from '@szhn/dh-design-pc'


function openTop() {
  Message({ message: '顶部消息', placement: 'top', type: 'info' })
}

function openBottom() {
  Message({ message: '底部消息', placement: 'bottom', type: 'success' })
}
</script>

<style scoped>
.demo-row {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}
</style>

全局方法

如果以插件方式全量注册,app.config.globalProperties 上会注入 $message,可在组件实例中直接调用。

按需引入

ts
import { Message } from '@szhn/dh-design-pc'

Message({ message: '这是一条消息提示' })
Message.success({ message: '保存成功' })
Message.closeAll()

应用上下文继承 (2.0.3)

消息方法支持接收当前应用的 appContext 作为第二个参数,以继承全局配置。

ts
import { getCurrentInstance } from 'vue'
import { Message } from '@szhn/dh-design-pc'

const { appContext } = getCurrentInstance()!
Message({}, appContext)

API

配置项(部分)

名称说明类型默认值
message消息文字string / VNode / Function''
type消息类型success / warning / info / error / primaryinfo
plain是否纯色背景booleanfalse
icon自定义图标string / Component
dangerouslyUseHTMLString是否把 message 作为 HTML 片段booleanfalse
duration展示时长(毫秒),为 0 则不自动关闭number3000
showClose是否展示关闭按钮booleanfalse
offset距离视口的偏移量number16
appendTo指定挂载根节点string / HTMLElement
grouping合并相同内容的消息booleanfalse
repeatNum合并场景下的初始重复次数number1
placement消息位置top / bottomtop
onClose关闭回调Function

方法

名称说明
Message.success / warning / info / error / primary展示对应类型的消息
Message.closeAll关闭所有消息实例
instance.close关闭当前消息实例

更多配置参见 Element Plus Message