更新记录
0.0.3(2023-06-06)
优化文档
0.0.2(2023-06-06)
一个基于Vue3和TypeScript开发的 Toast 组件,在页面中间弹出提示,用于消息通知、加载提示、操作结果提示等场景。
平台兼容性
Vue2 | Vue3 |
---|---|
× | √ |
App | 快应用 | 微信小程序 | 支付宝小程序 | 百度小程序 | 字节小程序 | QQ小程序 |
---|---|---|---|---|---|---|
HBuilderX 3.7.11 app-vue | × | √ | √ | × | × | √ |
钉钉小程序 | 快手小程序 | 飞书小程序 | 京东小程序 |
---|---|---|---|
√ | × | × | × |
H5-Safari | Android Browser | 微信浏览器(Android) | QQ浏览器(Android) | Chrome | IE | Edge | Firefox | PC-Safari |
---|---|---|---|---|---|---|---|---|
√ | √ | √ | √ | √ | √ | √ | √ | √ |
组件库官方文档:fant-mini-plus
组件库提供更多vue3组件,比单独引入更加强大
Toast 轻提示
在页面中间弹出提示,用于消息通知、加载提示、操作结果提示等场景。
代码演示
基础用法
<!-- 在页面内添加对应的节点 -->
<hd-toast></hd-toast>
<button type="primary" @click="open">开启toast</button>
<script lang="ts" setup>
import { ref } from 'vue'
import { useToast } from '@/uni_modules/hd-toast/components/hd-toast';
const toast = useToast()
function open() {
toast.showToast({
title: '打开了'
})
}
</script>
底色类型
通过type
设置底色类型;white
底色为白色,图标为多色;black
底色为黑色,图标为白色;默认为black
。
<button type="primary" @click="open">开启toast</button>
<hd-toast></hd-toast>
<script lang="ts" setup>
import { ref } from 'vue'
import { useToast } from '@/uni_modules/hd-toast/components/hd-toast';
const toast = useToast()
function open() {
toast.showToast({
title: '打开了',
type: 'white'
})
}
</script>
提示内容
通过title
设置提示内容。
<button type="primary" @click="open">开启toast</button>
<hd-toast ></hd-toast>
<script lang="ts" setup>
import { ref } from 'vue'
import { useToast } from '@/uni_modules/hd-toast/components/hd-toast';
const toast = useToast()
function open() {
toast.showToast({
title: '这里是提示内容'
})
}
</script>
提示图标
通过icon
设置提示图标;success
: 显示成功图标;warning
: 显示警告图标;error
: 显示错误图标;none
: 不显示图标。
<button type="primary" @click="open">开启toast</button>
<hd-toast ></hd-toast>
<script lang="ts" setup>
import { ref } from 'vue'
import { useToast } from '@/uni_modules/hd-toast/components/hd-toast';
const toast = useToast()
function open() {
toast.showToast({
title: '这里是提示内容',
icon: 'error'
})
}
</script>
自定义图标
通过image
设置自定义图标,优先级大于icon
。
<button type="primary" @click="open">开启toast</button>
<hd-toast ></hd-toast>
<script lang="ts" setup>
import { ref } from 'vue'
import { useToast } from '@/uni_modules/hd-toast/components/hd-toast';
const toast = useToast()
function open() {
toast.showToast({
title: '这里是提示内容', icon: 'error', image: ''
})
}
</script>
提示持续时间
通过duration
设置调整显示toast
时间,单位毫秒,默认 1500 毫秒。
<button type="primary" @click="open">开启toast</button>
<hd-toast ></hd-toast>
<script lang="ts" setup>
import { ref } from 'vue'
import { useToast } from '@/uni_modules/hd-toast/components/hd-toast';
const toast = useToast()
function open() {
toast.showToast({
title: '这里是提示内容', icon: 'error', duration: 2000
})
}
</script>
方法
fant-mini
中导出了以下 Toast
相关的辅助函数:
方法名 | 说明 | 参数 | 返回值 |
---|---|---|---|
showToast | 展示提示 | ToastOptions |
Toast 实例 |
hideToast | 关闭提示 | - |
- |
ToastOptions
数据结构
调用 showToast
方法时,支持传入以下选项:
参数 | 说明 | 类型 | 是否必填 | 默认值 |
---|---|---|---|---|
type | 底色类型,可选值:white black |
ToastType |
false |
black |
title | 提示的内容。 | String |
false |
- |
icon | 图标类型,可选值:none success warning error |
ToastIconType |
false |
none |
image | 自定义图标的本地路径 | String |
false |
- |
duration | 提示的持续时间 | Number |
false |
1500 |
Props
Name | Description | Type | Required | Default |
---|---|---|---|---|
value | toast是否展示 | Boolean |
false |
false |
duration | toast展示时长(ms),值为 0 时,notify 不会消失,默认值1500 | Number |
false |
1500 |
title | toast提示的内容 | String |
false |
- |
type | toast底色类型 | 'white' / 'black' |
false |
默认值是:black |
icon | toast图标 - success: 显示成功图标 - warning: 显示警告图标 - error: 显示错误图标 - none: 不显示图标 | 'success' / 'none' / 'warning' / 'error' |
false |
默认值是:none |
image | toast自定义图片 | String |
false |
- |
zIndex | 自定义层级,默认值 1000 | Number |
false |
1000 |
id | 组件唯一标识 | String |
false |
- |
Events
Event Name | Description | Parameters |
---|---|---|
update:visible | 消息展示状态变更时触发 | value:Boolean 消息展示状态,建议通过v-model双向绑定输入值,而不是监听此事件的形式 |
被点击 | 被点击时触发 | - |
联系方式
有不明白或者建议可以扫码交流