uniapp底部栏设置未读红点或角标

pages.json

javascript 复制代码
{
    ... // 省略
    "tabBar": {
        "color": "#333333",
         "selectedColor": "#3296fa",
         "backgroundColor": "#ffffff",
         "borderStyle": "white",
             "list": [
                 {
                     "pagePath": "pages/sys/workbench/index",
                     "iconPath": "static/images/tabbar/apply_1.png",
                     "selectedIconPath": "static/images/tabbar/apply_2.png",
                     "text": "首页"
                 },
                 {
                     "pagePath": "pages/sys/msg/index",
                     "iconPath": "static/images/tabbar/msg_1.png",
                     "selectedIconPath": "static/images/tabbar/msg_2.png",
                     "text": "消息"
                 },
                 {
                     "pagePath": "pages/sys/user/index",
                     "iconPath": "static/images/tabbar/my_1.png",
                     "selectedIconPath": "static/images/tabbar/my_2.png",
                     "text": "我的"
                 }
             ]
    }, // 底部按钮栏配置
}

workbench/index.vue(路径自定义,看具体业务)

javascript 复制代码
onShow() {
	this.getMessageInfo();
}

getMessageInfo() {
    this.$u.api.message.noticeAll().then((res) => {
        let count =
            res.data.earlyNum +
            res.data.newsNum +
            res.data.noticeNum +
            res.data.taskNum;
        if (count > 0) {
            uni.setTabBarBadge({
                index: 0,
                text: "11",
            });
            uni.setTabBarBadge({
                index: 1,
                text: "99+", 
            });
        } else {
            uni.removeTabBarBadge({
                index: 1, 
                text: "",
            });
        }
    });
},

API

uni.setTabBarBadge(OBJECT)
为 tabBar 某一项的右上角添加文本。

OBJECT参数说明:

参数 类型 必填 说明
index Number tabBar的哪一项,从左边算起
text String 显示的文本,不超过 3 个半角字符
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)
uni.removeTabBarBadge(OBJECT)
移除 tabBar 某一项右上角的文本。

OBJECT参数说明:

参数 类型 必填 说明
index Number tabBar的哪一项,从左边算起
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)
uni.showTabBarRedDot(OBJECT)
显示 tabBar 某一项的右上角的红点。

OBJECT参数说明:

参数 类型 必填 说明
index Number tabBar的哪一项,从左边算起
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)
uni.hideTabBarRedDot(OBJECT
隐藏 tabBar 某一项的右上角的红点。

OBJECT参数说明:

参数 类型 必填 说明
index Number tabBar的哪一项,从左边算起
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)
uni.onTabBarMidButtonTap(CALLBACK)
监听中间按钮的点击事件

Tip

  • tabbar是原生的,层级高于前端元素
  • uni-app插件市场有封装的前端tabbar,但性能不如原生tabbar
  • 如果想要一个中间带+号的tabbar,在HBuilderX中新建uni-app项目、选择 底部选项卡 模板
  • 以上大部分操作 tabbar 的 API 需要在 tabbar 渲染后才能使用,避免在 tabbar 未初始化前使
相关推荐
小雨cc5566ru7 小时前
uniapp+Android面向网络学习的时间管理工具软件 微信小程序
android·微信小程序·uni-app
二十雨辰13 小时前
[uni-app]小兔鲜-04推荐+分类+详情
前端·javascript·uni-app
小雨cc5566ru17 小时前
hbuilderx+uniapp+Android健身房管理系统 微信小程序z488g
android·微信小程序·uni-app
敲啊敲952717 小时前
uni-app之旅-day02-分类页面
前端·javascript·uni-app
二十雨辰17 小时前
[uni-app]小兔鲜-06地址+sku+购物车
前端·javascript·vue.js·uni-app
康康爹17 小时前
uniapp 小程序,登录上传头像昵称页面处理步骤
小程序·uni-app
小雨cc5566ru18 小时前
微信小程序hbuilderx+uniapp+Android 新农村综合风貌旅游展示平台
android·微信小程序·uni-app
小雨cc5566ru18 小时前
小程序 uniapp+Android+hbuilderx体育场地预约管理系统的设计与实现
android·小程序·uni-app
某公司摸鱼前端1 天前
uniapp 上了原生的 echarts 图表插件了 兼容性还行
前端·uni-app·echarts
貂蝉空大1 天前
uni-app 封装websocket 心跳检测,开箱即用
websocket·网络协议·uni-app