uni.setTabBarBadge 在非 tabbar 页面上更新无效问题

官方建议使用缓存,可以将购物车的有效数量通过 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)
},

猜你喜欢

转载自blog.csdn.net/QQ_Empire/article/details/128386809