官方建议使用缓存,可以将购物车的有效数量通过 vuex 的 getters 进行实时计算。
在每一个 tabbar 页面时都使用 onShow 钩子来将最新的购物车数量更新到 tabbar 角标
settabbar.js
import store from '@/store'
export default function tabBarBadgeSync() {
if (store.state.token) {
if (store.state.messageinfo.UnReadCNT < 1) {
return uni.removeTabBarBadge({
index: 2,
});
}
setTimeout(() => {
uni.setTabBarBadge({
index: 2,
text: store.state.messageinfo.UnReadCNT + "",
});
}, 100);
}
}
使用方式
import tabBarBadgeSync from '@/utils/settabbar.js';
onShow() {
tabBarBadgeSync.call(this)
},