Notify 通知
介绍
在页面顶部展示消息提示,用于操作反馈、状态提示等场景。
导入
js
import Notify, { type NotifyInstance } from '@/components/feedback/Notify.vue'用法
基础用法
通过 ref 获取 Notify 实例,然后调用实例的 show 方法来显示通知。
vue
<template>
<Notify ref="notify" />
<CellGroup title="基础用法" round inset>
<Cell title="显示通知" showArrow touchable @click="notify?.show({ content: '这是一个提示!' })" />
<Cell title="成功提示" showArrow touchable @click="notify?.show({ content: '这是一个成功提示!', type: 'success' })" />
<Cell title="失败提示" showArrow touchable @click="notify?.show({ content: '这是一个失败提示!', type: 'fail' })" />
<Cell title="无网络提示" showArrow touchable @click="notify?.show({ content: '网络不给力,请稍后再试', type: 'offline' })" />
<Cell title="自定义图标" showArrow touchable @click="notify?.show({ content: '自定义图标', icon: 'smile' })" />
</CellGroup>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import Notify, { type NotifyInstance } from '@/components/feedback/Notify.vue';
import Cell from '@/components/basic/Cell.vue';
import CellGroup from '@/components/basic/CellGroup.vue';
const notify = ref<NotifyInstance>();
</script>自定义配置
可以自定义通知的时长、按钮、样式等。
vue
<template>
<Notify ref="notify" />
<CellGroup title="自定义配置" round inset>
<Cell title="自定义时长" showArrow touchable @click="notify?.show({ content: '这是一个显示 8 秒的通知', duration: 8000 })" />
<Cell title="自定义按钮" showArrow touchable @click="showWithButton" />
<Cell title="自定义样式与颜色" showArrow touchable @click="showWithStyle" />
</CellGroup>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import Notify, { type NotifyInstance } from '@/components/feedback/Notify.vue';
import Cell from '@/components/basic/Cell.vue';
import CellGroup from '@/components/basic/CellGroup.vue';
const notify = ref<NotifyInstance>();
function showWithButton() {
notify.value?.show({
content: '您有一条新消息!',
button: '查看',
onButtonClick() {
uni.showModal({ content: '您点击了自定义按钮' });
},
});
}
function showWithStyle() {
notify.value?.show({
content: '这是一个自定义样式通知',
notifyStyle: {
backgroundColor: '#f61',
borderRadius: '10rpx',
},
textStyle: {
color: '#fff',
},
});
}
</script>高级用法
自定义渲染
通过 slot 可以自定义通知的渲染内容。
vue
<template>
<Notify ref="notify">
<template #item="{ tag, item }">
<template v-if="tag === 'ad1'">
<Text color="#f00" :size="60">¥10</Text>
<FlexCol>
<Text>最高领10元红包🎁</Text>
<Text>外卖爆款美食3.9元起</Text>
</FlexCol>
<Button type="danger" shape="round">立即领取</Button>
</template>
</template>
</Notify>
<Button @click="notify?.show({ tag: 'ad1', duration: 8000 })" type="primary">
显示自定义通知
</Button>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import Notify, { type NotifyInstance } from '@/components/feedback/Notify.vue';
import Text from '@/components/basic/Text.vue';
import FlexCol from '@/components/layout/FlexCol.vue';
import Button from '@/components/basic/Button.vue';
const notify = ref<NotifyInstance>();
</script>动态修改内容
vue
<template>
<Notify ref="notify" />
<Button @click="showAndUpdate" type="primary">
显示并更新通知
</Button>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import Notify, { type NotifyInstance } from '@/components/feedback/Notify.vue';
import Button from '@/components/basic/Button.vue';
const notify = ref<NotifyInstance>();
function showAndUpdate() {
const item = notify.value?.show({
content: '加载中请稍后',
type: 'loading',
duration: 0, // 不自动关闭
});
if (item) {
let time = 0;
const timer = setInterval(() => {
if (time < 8) {
time++;
item.updateProps({ content: `加载中 (${time}/8)...` });
} else {
item.close(); // 手动关闭
clearInterval(timer);
}
}, 1000);
}
}
</script>API 参考
Notify
通知组件,用于在页面顶部展示消息提示。
Props
| 名称 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| position | 提示显示位置 | 'top' | 'bottom' | 'center' | - | 'top' |
| closeOnClick | 点击后是否关闭通知 | boolean | - | false |
Slots
| 名称 | 说明 |
|---|---|
| item | 自定义通知项渲染,可通过 tag 区分不同的通知 |
| content | 自定义通知内容 |
实例方法
| 方法名 | 说明 | 参数 | 返回值 |
|---|---|---|---|
| show | 显示通知 | NotifyShowProps | NotifyItemInstance |
| closeAll | 关闭所有通知 | - | - |
NotifyShowProps
| 名称 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| id | 用于自定义渲染的唯一标识 | number | - | - |
| tag | 用于自定义渲染的标签 | string | - | - |
| duration | 自动关闭的延时,单位ms。为0不会自动关闭 | number | - | 4000 |
| type | 图标类型 | 'text' | 'loading' | 'success' | 'fail' | 'info' | 'offline' | - | 'text' |
| icon | 自定义图标 | string | - | - |
| content | 内容 | string | - | - |
| button | 按钮文字 | string | - | - |
| buttonProps | 按钮属性 | ButtonProp | - | - |
| iconProps | 图标自定义属性 | IconProps | - | - |
| closeOnClick | 是否在点击后关闭 | boolean | - | false |
| notifyStyle | 提示容器的自定义样式 | ViewStyle | - | - |
| textStyle | 提示文字的自定义样式 | TextStyle | - | - |
| onButtonClick | 按钮点击回调 | (e: any) => void | - | - |
| onClose | 关闭后回调 | () => void | - | - |
| onClick | 点击回调 | () => void | - | - |
NotifyItemInstance
| 方法名 | 说明 | 参数 | 返回值 |
|---|---|---|---|
| updateProps | 更新通知属性 | Omit<NotifyShowProps, 'duration'|'onClose'> | - |
| close | 关闭通知 | - | - |
主题变量
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| NotifyBackgroundColor | string | background.notify | 默认背景颜色 |
| NotifySuccessBackgroundColor | string | background.success | 成功状态背景颜色 |
| NotifyFailBackgroundColor | string | background.danger | 失败状态背景颜色 |
| NotifyInfoBackgroundColor | string | background.info | 信息状态背景颜色 |
| NotifyOfflineBackgroundColor | string | background.warning | 离线状态背景颜色 |
| NotifyTextColor | string | text.content | 文本颜色 |
| NotifyIconSuccess | string | success | 成功图标 |
| NotifyIconError | string | error | 错误图标 |
| NotifyIconOffline | string | cry | 离线图标 |
| NotifyIconInfo | string | prompt | 信息图标 |
| NotifyIconSize | number | 45 | 图标大小 |
| NotifyIconMarginBottom | number | 12 | 图标底部间距 |
| NotifyPaddingVertical | number | 25 | 垂直内边距 |
| NotifyPaddingHorizontal | number | 30 | 水平内边距 |
| NotifyRadius | number | 16 | 圆角大小 |
| NotifyGap | number | 21 | 通知内部元素间距 |
| NotifyContainerGap | number | 15 | 通知容器内间距 |
| NotifyMarginWhenTop | number | 100 | 顶部位置时的上边距 |
| NotifyMarginWhenBottom | number | 100 | 底部位置时的下边距 |