公式の推奨事項は、 vuex getter を介してリアルタイムでショッピング カートの有効数を計算できるキャッシングを使用することです。
各タブバー ページで onShow フックを使用して、最新のカートの数量をタブバー コーナーに更新します。
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)
},