Skip to content

Notification 通知 ​

基础用法 ​

全局通知组件,从页面顶部或底部弹出带标题与内容的通知卡片,适合订单状态、系统消息等重要提醒。

  • 通知以队列形式堆叠,弹出位置可选页面顶部或底部
  • 支持自动关闭、剩余时间进度条、关闭按钮与相同通知合并计数
  • 支持手势拖动关闭,可关闭单条通知或一次清空全部

必须放在 sn-page 下

弹出层类组件需直接放在 sn-page 根节点下使用,否则主题颜色与动画时长等 CSS 变量无法作用到组件内部。

平台差异

HarmonyOS 端通知卡片默认阴影较轻。

vue
<template>
	<sn-page>
		<sn-button text="显示通知" type="primary" @click="showNotice"></sn-button>
		<sn-notification ref="notificationRef"></sn-notification>
	</sn-page>
</template>

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

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="ts" setup>
import type { SnNotificationParams } from '@/uni_modules/sinle-ui/core/types/index.ts'

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>

拖动关闭 ​

开启 draggable 后按住通知左右拖动可跟手移动,松手未达阈值时回弹;拖动期间自动关闭计时暂停,Web 端同时支持鼠标拖拽。

实现原理 ​

卡片图标位于按 type 与 level 着色的圆角色块中,默认图标随类型自动匹配;配色等级与 sn-button 的 type / level 体系一致。

属性 ​

参数说明类型默认值可选值
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图标颜色,支持 $ 简写,按 type 与 level 取主题色String--
titleSize标题字号,支持 $ 简写String | Number$15-
contentSize内容字号,支持 $ 简写String | Number$13-
borderRadius卡片圆角大小,支持 $ 简写String | Number$12-
draggable是否开启手势拖动关闭Booleanfalsetrue | false
dragThreshold横向拖动关闭阈值,超过则飞出关闭,支持数字(px)与百分比字符串(相对卡片宽度)String | Number80-
customStyle自定义通知卡片样式SnStyleValue''-
customClass通知卡片外部类String''-
titleStyle自定义标题样式SnStyleValue''-
titleClass标题外部类String''-
contentStyle自定义内容样式SnStyleValue''-
contentClass内容外部类String''-
iconStyle自定义图标样式SnStyleValue''-
iconClass图标外部类String''-
closeStyle自定义关闭按钮样式SnStyleValue''-
closeClass关闭按钮外部类String''-
badgeStyle自定义合并计数徽标样式SnStyleValue''-
badgeClass合并计数徽标外部类String''-
actionStyle自定义操作插槽容器样式SnStyleValue''-
actionClass操作插槽容器外部类String''-

事件 ​

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

方法 ​

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

sn-notification 组件 add 方法入参:title、content、type、level、position('top' | 'bottom')、duration、showProgress、showIcon、icon、closable、showAction(是否显示右侧操作插槽)、grouping、bgColor、titleColor、contentColor、iconColor、borderRadius、draggable、dragThreshold、customStyle。


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自定义通知卡片样式SnStyleValue | null

消息 / 通知类型。

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

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

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

消息条 / 通知队列位置。

可选值备注
top顶部
bottom底部

插槽 ​

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

使用 MIT 协议