::: danger 问题
uni-app setTabBarBadge在非tabBar页面设置时无法生效
:::
刚开始是在vuex里面使用getters获取商品总数 在app.vue里面watch
当前商品数量然后对应更新页面的角标
store/shoppingCart/shopping.js
getters:{
// 商品总量
goodsCount(state){
let num = 0
num = state.shoppingCart.reduce((count,current) => {
if(current.select && !state.outIdList.find(i => i == current.spuId)) {
return num += current.quantity
}else {
return num
}
},num)
console.log('num',num);
return num
}
}
app.vue
watch:{
goodsCount:{
handler(newGoodCount,oldGoodCount){
console.log('触发监听',newGoodCount);
let that = this
uni.setTabBarBadge({ //显示红点
index: 2 ,//tabbar下标
text:newGoodCount+'',
})
if(newGoodCount == 0) {
uni.hideTabBarRedDot({
index:2
})
}
},
immediate:true
}
},
后来控制台看是可以打印出来最新数据也执行了对应的逻辑 但是角标不更新
定义updateBadge
函数每个页面onShow
钩子里面调用该函数
index.vue
onShow(){
this.updateBadge()
},
updateBadge(){
if(this.goodsCount > 0) {
uni.setTabBarBadge({ //显示红点
index: 2 ,//tabbar下标
text:this.goodsCount+'',
})
}
if(this.goodsCount == 0) {
uni.hideTabBarRedDot({
index:2
})
}
},
问题解决😛
文章标题:uni-app setTabBarBadge在非tabBar页面设置时无法生效
文章作者:未知用户
文章链接:[复制]
最后修改时间:2024年 03月 01日 14时38分
商业转载请联系站长获得授权,非商业转载请注明本文出处及文章链接,您可以自由地在任何媒体以任何形式复制和分发作品,也可以修改和创作,但是分发衍生作品时必须采用相同的许可协议。 本文采用CC BY-NC-SA 4.0进行许可。
Copyright © 2023--2025
豫ICP备2022014268号-1
我们历经沧桑,这样错过了一生最好的时光,错过了便是永远