uni-app setTabBarBadge在非tabBar页面设置时无法生效

项目场景:

在这里插入图片描述

uniapp中使用vuex来保存userInfo,userInfo中含有购物车数量的值,在setUserInfo方法中中进行判断

if (Number(state.userInfo.cartGoodsNum) > 0) {
				uni.setTabBarBadge({
					index: 2,
					text: String(state.userInfo.cartGoodsNum),
					complete(e) {
						console.log(e);
					}
				});
			} else {
				uni.hideTabBarRedDot({
					index: 2
				});
			}

问题描述:

底部导航购物车需要有数量TabBarBadge
setTabBarBadge在非tabBar页面设置时无法生效
即商品详情页中,点击加入购物车

this.setUserInfo({
	cartGoodsNum: parseInt(this.userInfo.cartGoodsNum) + this.goodsNum
		});

返回页面时tabBar上的数量标识没有进行实时更改
提示错误信息{errMsg: "setTabBarBadge:fail not TabBar page"}


解决方案:

在每一个tabBar页面中的onShow()中都加上购物车数量判断

//页面显示
	onShow() {
		this.judgeLogin(() => {
			this.pageData();
			if (Number(this.userInfo.cartGoodsNum) > 0) {
				uni.setTabBarBadge({
					index: 2,
					text: String(this.userInfo.cartGoodsNum),
					complete(e) {
						console.log(e);
					}
				});
			} else {
				uni.hideTabBarRedDot({
					index: 2
				});
			}
		});
	},

就可以在页面返回的时候,重新进行判断,实时更新数量

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值