介绍
在页面顶部展示消息通知。
引入
js
import Notify from 'sard-uniapp/components/notify/notify.vue'
import NotifyAgent from 'sard-uniapp/components/notify-agent/notify-agent.vue'
import { notify } from 'sard-uniapp'代码演示
基础使用
先在页面放置代理组件。
tsx
<sar-notify-agent />接着便可以使用 notify 等方法显示提示。
vue
<template>
<sar-list card>
<sar-list-item arrow hover title="显示通知" @click="showNotice" />
<sar-list-item arrow hover title="隐藏通知" @click="hideNotice" />
</sar-list>
</template>
<script setup lang="ts">
import { notify } from 'sard-uniapp'
const showNotice = () => {
notify('这是一条通知')
}
const hideNotice = () => {
notify.hide()
}
</script>vue
<template>
<sar-list card>
<sar-list-item arrow hover title="显示通知" @click="showNotice" />
<sar-list-item arrow hover title="隐藏通知" @click="hideNotice" />
</sar-list>
</template>
<script setup lang="js">
import { notify } from "sard-uniapp";
const showNotice = () => {
notify("\u8FD9\u662F\u4E00\u6761\u901A\u77E5");
};
const hideNotice = () => {
notify.hide();
};
</script>类型
支持 primary、success、warning、error 四种通知类型,默认为 primary
vue
<template>
<sar-list card>
<sar-list-item arrow hover title="成功通知" @click="showSucessNotice" />
<sar-list-item arrow hover title="警告通知" @click="showWarningNotice" />
<sar-list-item arrow hover title="错误通知" @click="showErrorNotice" />
<sar-list-item arrow hover title="隐藏通知" @click="hideNotice" />
</sar-list>
</template>
<script setup lang="ts">
import { notify } from 'sard-uniapp'
const showSucessNotice = () => {
notify.success('这是一条通知')
}
const showWarningNotice = () => {
notify.warning('这是一条通知')
}
const showErrorNotice = () => {
notify.error('这是一条通知')
}
const hideNotice = () => notify.hide()
</script>vue
<template>
<sar-list card>
<sar-list-item arrow hover title="成功通知" @click="showSucessNotice" />
<sar-list-item arrow hover title="警告通知" @click="showWarningNotice" />
<sar-list-item arrow hover title="错误通知" @click="showErrorNotice" />
<sar-list-item arrow hover title="隐藏通知" @click="hideNotice" />
</sar-list>
</template>
<script setup lang="js">
import { notify } from "sard-uniapp";
const showSucessNotice = () => {
notify.success("\u8FD9\u662F\u4E00\u6761\u901A\u77E5");
};
const showWarningNotice = () => {
notify.warning("\u8FD9\u662F\u4E00\u6761\u901A\u77E5");
};
const showErrorNotice = () => {
notify.error("\u8FD9\u662F\u4E00\u6761\u901A\u77E5");
};
const hideNotice = () => notify.hide();
</script>自定义颜色
通过 color 属性设置文本颜色,通过 background 属性设置背景色。
vue
<template>
<sar-list card>
<sar-list-item arrow hover title="显示通知" @click="showNotice" />
<sar-list-item arrow hover title="隐藏通知" @click="hideNotice" />
</sar-list>
</template>
<script setup lang="ts">
import { notify } from 'sard-uniapp'
const showNotice = () => {
notify('这是一条通知', {
background: 'var(--sar-orange)',
})
}
const hideNotice = () => {
notify.hide()
}
</script>vue
<template>
<sar-list card>
<sar-list-item arrow hover title="显示通知" @click="showNotice" />
<sar-list-item arrow hover title="隐藏通知" @click="hideNotice" />
</sar-list>
</template>
<script setup lang="js">
import { notify } from "sard-uniapp";
const showNotice = () => {
notify("\u8FD9\u662F\u4E00\u6761\u901A\u77E5", {
background: "var(--sar-orange)"
});
};
const hideNotice = () => {
notify.hide();
};
</script>自定义时长
timeout 设为 0 后会一直显示。
vue
<template>
<sar-list card>
<sar-list-item arrow hover title="显示不会消失的通知" @click="showNotice" />
<sar-list-item arrow hover title="隐藏通知" @click="hideNotice" />
</sar-list>
</template>
<script setup lang="ts">
import { notify } from 'sard-uniapp'
const showNotice = () => {
notify('这是一条通知', {
timeout: 0,
})
}
const hideNotice = () => {
notify.hide()
}
</script>vue
<template>
<sar-list card>
<sar-list-item arrow hover title="显示不会消失的通知" @click="showNotice" />
<sar-list-item arrow hover title="隐藏通知" @click="hideNotice" />
</sar-list>
</template>
<script setup lang="js">
import { notify } from "sard-uniapp";
const showNotice = () => {
notify("\u8FD9\u662F\u4E00\u6761\u901A\u77E5", {
timeout: 0
});
};
const hideNotice = () => {
notify.hide();
};
</script>自定义位置
通知允许在屏幕上边或下边展示。
vue
<template>
<sar-list card>
<sar-list-item arrow hover title="在底部显示通知" @click="showNotice" />
<sar-list-item arrow hover title="隐藏通知" @click="hideNotice" />
</sar-list>
</template>
<script setup lang="ts">
import { notify } from 'sard-uniapp'
const showNotice = () => {
notify('这是一条通知', {
position: 'bottom',
})
}
const hideNotice = () => {
notify.hide()
}
</script>vue
<template>
<sar-list card>
<sar-list-item arrow hover title="在底部显示通知" @click="showNotice" />
<sar-list-item arrow hover title="隐藏通知" @click="hideNotice" />
</sar-list>
</template>
<script setup lang="js">
import { notify } from "sard-uniapp";
const showNotice = () => {
notify("\u8FD9\u662F\u4E00\u6761\u901A\u77E5", {
position: "bottom"
});
};
const hideNotice = () => {
notify.hide();
};
</script>API
NotifyProps
| 属性 | 描述 | 类型 | 默认值 |
|---|---|---|---|
| type | 加载类型 | 'primary' | 'success' | 'warning' | 'error' | 'primary' |
| message | 通知内容 | string | - |
| color | 字体颜色 | string | - |
| background | 背景色 | string | - |
| visible (v-model) | 是否显示通知 | boolean | - |
| position | 通知放置的位置 | 'top' | 'bottom' | 'top' |
| timeout | 展示时长(ms),值为 0 时,通知不会消失 | number | 3000 |
| duration | 显隐动画时长,单位 ms | number | 300 |
| status-bar 1.12+ | 是否包含状态栏,自定义导航栏时应设为 true | boolean | false |
NotifySlots 1.20.2+
| 插槽 | 描述 | 属性 |
|---|---|---|
| default | 自定义默认内容 | - |
NotifyEmits
| 事件 | 描述 | 类型 |
|---|---|---|
| update:visible | 通知框显隐时触发 | (visible: boolean) => void |
| click 1.20.2+ | 点击通知框时触发 | (event: any) => void |
| visible-hook 1.20.2+ | 入场/退场动画状态改变时触发 | (name: TransitionHookName) => void |
| before-enter 1.20.2+ | 入场动画开始前触发 | () => void |
| enter 1.20.2+ | 入场动画开始时触发 | () => void |
| after-enter 1.20.2+ | 入场动画结束时触发 | () => void |
| enter-cancelled 1.20.2+ | 入场动画取消时触发 | () => void |
| before-leave 1.20.2+ | 退场动画开始前触发 | () => void |
| leave 1.20.2+ | 退场动画开始时触发 | () => void |
| after-leave 1.20.2+ | 退场动画结束时触发 | () => void |
| leave-cancelled 1.20.2+ | 退场动画取消时触发 | () => void |
NotifyAgentProps / NotifyOptions
继承 NotifyProps 并有以下额外属性。
| 属性 | 描述 | 类型 | 默认值 |
|---|---|---|---|
| id | 通知组件的 id | string | 'notify' |
| onVisibleHook 1.20.2+ | 入场/退场动画状态改变时调用 | (name: TransitionHookName) => void | |
| onBeforeEnter 1.20.2+ | 入场动画开始前调用 | () => void | |
| onEnter 1.20.2+ | 入场动画开始时调用 | () => void | |
| onAfterEnter 1.20.2+ | 入场动画结束时调用 | () => void | |
| onEnterCancelled 1.20.2+ | 入场动画取消时调用 | () => void | |
| onBeforeLeave 1.20.2+ | 退场动画开始前调用 | () => void | |
| onLeave 1.20.2+ | 退场动画开始时调用 | () => void | |
| onAfterLeave 1.20.2+ | 退场动画结束时调用 | () => void | |
| onLeaveCancelled 1.20.2+ | 退场动画取消时调用 | () => void |
NotifyAgentEmits 1.20.2+
继承 NotifyEmits。
命令式方法
| 名称 | 描述 | 类型 |
|---|---|---|
| notify | 显示通知 | NotifyFunction |
| notify.success | 显示成功类型通知 | NotifySimpleShowFunction |
| notify.warning | 显示警告类型通知 | NotifySimpleShowFunction |
| notify.error | 显示错误类型通知 | NotifySimpleShowFunction |
| notify.hide | 隐藏指定 id 的命令式通知 | (id = 'notify') => void |
| notify.hideAll | 隐藏所有命令式通知 | () => void |
NotifyFunction
ts
type NotifyFunction = NotifySimpleShowFunction & {
success: NotifySimpleShowFunction
warning: NotifySimpleShowFunction
error: NotifySimpleShowFunction
hide: (id?: string) => void
hideAll: () => void
}NotifySimpleShowFunction
ts
interface NotifySimpleShowFunction {
(options: NotifyOptions): void
(message: string, options?: NotifyOptions): void
}主题定制
CSS 变量
scss
page,
.sar-portal {
--sar-notify-paddding-x: 32rpx;
--sar-notify-paddding-y: 16rpx;
--sar-notify-font-size: var(--sar-text-base);
--sar-notify-color: var(--sar-white);
}