Skip to content

Message 消息条

基础用法

  • 全局消息条组件,在页面顶部 / 底部弹出轻量消息提示,支持多条消息依次排列成队列,自动消失。
  • 弹出层类组件,必须直接放在 sn-page 根节点下使用,否则主题颜色与动画时长等 CSS 变量无法作用到组件内部。
  • 通过 ref 调用 show 方法弹出消息(参数类型 SnMessageParams),返回消息 id;调用 close(id) 关闭指定消息,close() 关闭全部消息。
  • type 提供五种功能色类型(info / primary / success / error / warning),默认图标随类型自动匹配;level 提供三种深浅等级(first / second / third),配色与 sn-button 的 type / level 体系一致。
  • position 控制消息队列整体位置:top-left / top / top-right / bottom-left / bottom / bottom-right 六个方向。
  • closable 显示右侧关闭按钮;grouping 开启后相同内容(同位置、同类型、同文字)的消息自动合并并显示计数徽标;duration 为自动关闭时长(ms),0 表示不自动关闭。
  • 组件属性作为 show() 参数的默认配置,show() 传入的参数优先级更高。
vue
<template>
	<sn-page>
		<sn-button text="显示消息" type="primary" @click="showMessage"></sn-button>
		<sn-message ref="messageRef"></sn-message>
	</sn-page>
</template>

<script lang="uts" setup>
import type { SnMessageParams } from '@/uni_modules/sinle-ui/core/types/index.uts'

const messageRef = ref<SnMessageComponentPublicInstance | null>(null)

function showMessage(): void {
	messageRef.value?.$callMethod('show', {
		text: '这是一条消息提示',
		type: 'success'
	} as SnMessageParams)
}
</script>

更多演示请下载 demo 查看

消息合并与手动关闭

vue
<template>
	<sn-message ref="messageRef" @close="onMessageClose"></sn-message>
</template>

<script lang="uts" setup>
import type { SnMessageParams } from '@/uni_modules/sinle-ui/core/types/index.uts'

const messageRef = ref<SnMessageComponentPublicInstance | null>(null)
let msgId = ''

function show(): void {
	msgId = messageRef.value?.$callMethod('show', {
		text: '这条消息可通过 close(id) 提前关闭',
		type: 'primary',
		closable: true,
		duration: 30000
	} as SnMessageParams) as string
}

function close(): void {
	messageRef.value?.$callMethod('close', msgId)
}

function onMessageClose(): void {
	console.log('有一条消息被关闭')
}
</script>

属性

参数说明类型默认值可选值
text默认消息文字String''-
type消息类型(功能色)Stringinfoinfo | primary | success | error | warning
level消息配色等级Stringfirstfirst | second | third
position消息队列整体位置Stringtoptop-left | top | top-right | bottom-left | bottom | bottom-right
duration自动关闭时长(ms),0 表示不自动关闭Number3000-
showIcon是否显示类型图标Booleantruetrue | false
icon自定义图标名称,为空时按 type 使用默认图标String''-
closable是否显示右侧关闭按钮Booleanfalsetrue | false
grouping是否开启消息合并,相同内容自动合并计数Booleanfalsetrue | false
bgColor消息条背景颜色,支持 $ 简写,为空时按 typelevel 取主题色String''-
textColor消息文字颜色,支持 $ 简写,为空时按 typelevel 取主题色String''-
iconColor图标颜色,支持 $ 简写,为空时跟随文字颜色String''-
textSize消息文字大小,支持 $ 简写,为空时随字体乘数缩放(约 14px)String | Number''-
borderRadius消息条圆角大小,支持 $ 简写,为空时随圆角乘数缩放(约 8px)String | Number''-
customStyle自定义消息条样式UTSJSONObject | String''-
customClass消息条外部类String''-
textStyle自定义消息文字样式UTSJSONObject | String''-
textClass消息文字外部类String''-
iconStyle自定义图标样式UTSJSONObject | String''-
iconClass图标外部类String''-
closeStyle自定义关闭按钮样式UTSJSONObject | String''-
closeClass关闭按钮外部类String''-
badgeStyle自定义合并计数徽标样式UTSJSONObject | String''-
badgeClass合并计数徽标外部类String''-

事件

名称类型说明
close() => Void有一条消息被关闭(移除)时触发

方法

名称参数返回值描述
showparam: SnMessageParams | nullString弹出一条消息,参数字段优先于组件属性默认值,返回消息 id
closeid: String(默认 ''-关闭消息;传入 id 时仅关闭对应消息,留空时关闭全部消息

sn-message 组件 add 方法入参:text(内容)、typelevelpositiondurationshowIconiconclosablegrouping(相同内容归并计数)、bgColortextColoriconColortextSizeborderRadiuscustomStyle

show 参数(SnMessageParams)

字段说明类型
text消息文字String | null
type消息类型SnMessageType | null
level配色等级SnMessageLevel | null
position弹出位置SnMessagePosition | null
duration自动关闭时长(ms)Number | null
showIcon是否显示类型图标Boolean | null
icon自定义图标名称String | null
closable是否显示关闭按钮Boolean | null
grouping是否开启消息合并Boolean | null
bgColor背景颜色String | null
textColor文字颜色String | null
iconColor图标颜色String | null
textSize文字大小String | null
borderRadius圆角大小String | null
customStyle自定义消息条样式UTSJSONObject | null

消息 / 通知类型。

可选值备注
primary主色
success成功
error错误
info信息
warning警告

消息 / 通知等级(表现程度)。

可选值备注
first一级(表现最重)
second二级
third三级(表现最轻)

消息条 / 通知队列位置。

可选值备注
top-left左上
top顶部
top-right右上
bottom-left左下
bottom底部
bottom-right右下

使用 MIT 协议