Skip to content

Notification 通知

基础用法

  • 全局通知组件,从页面顶部 / 底部弹出带标题与内容的通知卡片,适合订单状态、系统消息等重要提醒。
  • 弹出层类组件,必须直接放在 sn-page 根节点下使用,否则主题颜色与动画时长等 CSS 变量无法作用到组件内部。
  • 通过 ref 调用 show 方法弹出通知(参数类型 SnNotificationParams),返回通知 id;调用 close(id) 关闭指定通知,close() 关闭全部通知。
  • type 提供五种功能色类型(info / primary / success / error / warning),默认图标随类型自动匹配,图标位于按类型与等级着色的圆角色块中;level 提供三种配色等级(first / second / third),与 sn-button 的 type / level 体系一致。
  • position 控制通知队列整体位置(top / bottom);show-progress 开启卡片底部剩余时间进度条;closable 显示右侧关闭按钮;grouping 开启后相同标题 + 内容的通知自动合并计数。
  • draggable 开启手势拖动:按住通知左右拖动可跟手移动,松手超过阈值(drag-threshold,默认 80px,支持卡片宽度百分比)向对应方向飞出关闭,未达阈值回弹;拖动期间自动关闭计时暂停,Web 端同时支持鼠标拖拽。
  • 组件属性作为 show() 参数的默认配置,show() 传入的参数优先级更高。
vue
<template>
	<sn-page>
		<sn-button text="显示通知" type="primary" @click="showNotice"></sn-button>
		<sn-notification ref="notificationRef"></sn-notification>
	</sn-page>
</template>

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

const notificationRef = ref<SnNotificationComponentPublicInstance | null>(null)

function showNotice(): void {
	notificationRef.value?.$callMethod('show', {
		title: '订单支付成功',
		content: '您的订单已支付成功,预计 3 天内发货。',
		type: 'success'
	} as SnNotificationParams)
}
</script>

更多演示请下载 demo 查看

操作插槽与手动关闭

show() 参数中设置 showAction: true 时显示右侧操作插槽,插槽作用域暴露当前通知 id

vue
<template>
	<sn-notification ref="notificationRef">
		<template v-slot:action="slotProps">
			<sn-button type="primary" level="least" text="复制" @click="onCopy(slotProps.id)"></sn-button>
		</template>
	</sn-notification>
</template>

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

const notificationRef = ref<SnNotificationComponentPublicInstance | null>(null)

function show(): void {
	notificationRef.value?.$callMethod('show', {
		title: '订单待支付',
		content: '订单号 SN20260101001,请尽快完成支付。',
		type: 'warning',
		duration: 10000,
		showAction: true
	} as SnNotificationParams)
}

function onCopy(id: string): void {
	uni.setClipboardData({ data: '订单号 SN20260101001' })
	notificationRef.value?.$callMethod('close', id)
}
</script>

属性

参数说明类型默认值可选值
title默认通知标题String''-
content默认通知内容String''-
type通知类型(功能色)Stringinfoinfo | primary | success | error | warning
level通知配色等级Stringfirstfirst | second | third
position通知队列整体位置Stringtoptop | bottom
duration自动关闭时长(ms),0 表示不自动关闭Number4500-
showProgress是否显示卡片底部剩余时间进度条Booleanfalsetrue | false
showIcon是否显示类型图标Booleantruetrue | false
icon自定义图标名称,为空时按 type 使用默认图标String''-
closable是否显示右侧关闭按钮Booleanfalsetrue | false
grouping是否开启通知合并,相同标题 + 内容自动合并计数Booleanfalsetrue | false
bgColor通知卡片背景颜色,支持 $ 简写String$front-
titleColor标题颜色,支持 $ 简写String$title-
contentColor内容颜色,支持 $ 简写String$text-
iconColor图标颜色,支持 $ 简写,为空时按 typelevel 取主题色String''-
titleSize标题字号,支持 $ 简写,为空时随字体乘数缩放(约 15px)String | Number''-
contentSize内容字号,支持 $ 简写,为空时随字体乘数缩放(约 13px)String | Number''-
borderRadius卡片圆角大小,支持 $ 简写,为空时随圆角乘数缩放(约 12px)String | Number''-
draggable是否开启手势拖动关闭Booleanfalsetrue | false
dragThreshold横向拖动关闭阈值,超过则飞出关闭,支持数字(px)与百分比字符串(相对卡片宽度)String | Number80-
customStyle自定义通知卡片样式UTSJSONObject | String''-
customClass通知卡片外部类String''-
titleStyle自定义标题样式UTSJSONObject | String''-
titleClass标题外部类String''-
contentStyle自定义内容样式UTSJSONObject | String''-
contentClass内容外部类String''-
iconStyle自定义图标样式UTSJSONObject | String''-
iconClass图标外部类String''-
closeStyle自定义关闭按钮样式UTSJSONObject | String''-
closeClass关闭按钮外部类String''-
badgeStyle自定义合并计数徽标样式UTSJSONObject | String''-
badgeClass合并计数徽标外部类String''-
actionStyle自定义操作插槽容器样式UTSJSONObject | String''-
actionClass操作插槽容器外部类String''-

事件

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

方法

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

sn-notification 组件 add 方法入参:titlecontenttypelevelposition'top' | 'bottom')、durationshowProgressshowIconiconclosableshowAction(是否显示右侧操作插槽)、groupingbgColortitleColorcontentColoriconColorborderRadiusdraggabledragThresholdcustomStyle


show 参数(SnNotificationParams)

字段说明类型
title通知标题String | null
content通知内容String | null
type通知类型SnNotificationType | null
level配色等级SnNotificationLevel | null
position弹出位置SnNotificationPosition | null
duration自动关闭时长(ms)Number | null
showProgress是否显示进度条Boolean | null
showIcon是否显示类型图标Boolean | null
icon自定义图标名称String | null
closable是否显示关闭按钮Boolean | null
showAction是否显示右侧操作插槽(需组件内定义插槽内容)Boolean | null
grouping是否开启通知合并Boolean | null
bgColor卡片背景颜色String | null
titleColor标题颜色String | null
contentColor内容颜色String | null
iconColor图标颜色String | null
borderRadius圆角大小String | null
draggable是否开启手势拖动关闭Boolean | null
dragThreshold拖动关闭阈值String | Number | null
customStyle自定义通知卡片样式UTSJSONObject | null

消息 / 通知类型。

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

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

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

消息条 / 通知队列位置。

可选值备注
top顶部
bottom底部

插槽

名称说明
action卡片右侧操作区插槽,仅当 show() 参数 showActiontrue 时显示;作用域暴露 id(当前通知 id)

使用 MIT 协议