Skip to content
# t-notify

Notify 消息通知

t-notify 组件介绍 ​

消息通知组件,用于在页面上显示通知消息,支持配置消息类型、文本、持续时间等。

t-notify 平台兼容 ​

HarmonyH5AndroidiOS小程序UTS
✅✅✅✅✅✅

t-notify 基础用法 ​

基础用法参考 demo/notify/notify.uvue,通过 ref 获取组件实例并调用 add 方法添加通知。

vue
<template>
	<view>
		<tui-theme-switch></tui-theme-switch>
		<t-card class="mltr-15"
				title='Notice 通知提示列表'
				sub-title="..."></t-card>
		<introduction title="基础功能">
			<t-button type="primary"
					  @click="addMessage">随机一条默认配置消息</t-button>
		</introduction>
		<introduction title="设置图标">
			<t-button type="error"
					  @click="addMessage1('close-circle')">错误</t-button>
		</introduction>
		<introduction title="插槽测试">
			<t-button type="primary"
					  class="mb-15"
					  @click="addMessage2">show</t-button>
		</introduction>
		<t-notify ref="notify"></t-notify>

		<t-notify ref="notifyslot">
			<view :class="[`tbg-pn h-35 fc r-5 mb-20`]">
				<text :class="[`tfg-pn`]">插槽测试</text>
			</view>
		</t-notify>
	</view>
</template>

<script setup>
	import { ref } from 'vue'
	import type { ComponentPublicInstance } from 'vue'

	// 定义 Refs
	const notify = ref<ComponentPublicInstance | null>(null)
	const notifyslot = ref<ComponentPublicInstance | null>(null)

	// 基础数据
	const list = ['欢迎使用Tui', '欢迎使用Tui', '晓云含雨却低徊', '大江东去浪淘尽']
	const typeList = ['primary', 'error', 'warning', 'success']

	// 随机数生成函数
	const random = (min : number, max : number) : number => {
		if (min >= 0 && max > 0 && max >= min) {
			const gab = max - min + 1
			return Math.floor(Math.random() * gab + min)
		}
		return 0
	}

	// 获取随机文本
	const getText = () => list[random(0, 3)] ?? ''

	// 获取随机类型
	const getType = () => typeList[random(0, 3)] ?? 'primary'

	// 随机默认配置消息
	const addMessage = () => {
		notify.value?.$callMethod('add', {
			type: getType(),
			text: getText(),
			duration: 1000
		})
	}

	// 插槽测试
	const addMessage2 = () => {
		notifyslot.value?.$callMethod('add', { duration: 1000 })
	}

	// 设置图标
	const addMessage1 = (icon : string) => {
		notify.value?.$callMethod('add', {
			type: getType(),
			text: getText(),
			duration: 1000,
			icon
		})
	}
</script>

t-notify Props ​

属性名类型默认值说明可选值
sizeString""组件尺寸large, medium, small, mini
typeString""组件类型info, primary, error, warning, success
disabledBooleanfalse组件是否禁用true, false
hoverBooleanfalse是否有点击效果true, false
pathString""点击组件后跳转的页面路径-
effectString"normal"组件显示主题normal, dark, light, plain

t-notify Events ​

事件名说明回调参数
click点击时触发-
transitionend动画结束时触发-
initFinished组件初始化完成触发组件的节点信息
touchstart触摸开始时触发触摸事件对象
touchend触摸结束时触发触摸事件对象
touchmove触摸移动时触发触摸事件对象
touchcancel触摸取消时触发触摸事件对象

Methods ​

方法名说明参数返回值
add添加通知option: UTSJSONObject无

Add 方法参数 ​

属性名类型说明
typeString通知类型:info, primary, error, warning, success
textString通知文本内容
durationNumber显示持续时间,单位为毫秒
iconString通知图标名称
sizeString通知尺寸
colorString文字颜色
backgroundColorString背景颜色

t-notify Slots ​

插槽名说明
default内容插槽,用于自定义通知内容