Files
zt-closet-wxmini-frontend/miniprogram/utils/tabbar.ts
T
2026-09-13 01:51:09 +08:00

19 lines
774 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 自定义 tabBar 实例获取:只有 tab 页自身才有 getTabBar(),
// 通知页等非 tab 页调用会拿到 undefined,需从页面栈中查找仍在栈内的 tab 页。
export function getTabBarInstance(): any {
const pages = typeof getCurrentPages === 'function' ? getCurrentPages() : []
for (let i = pages.length - 1; i >= 0; i--) {
const page = pages[i] as any
const tabBar = page && page.getTabBar && page.getTabBar()
if (tabBar) return tabBar
}
return null
}
// 同步未读红点:优先让 tabBar 重新拉取真实数量,取不到实例时静默跳过
export function syncTabBarUnread(force = true): void {
const tabBar = getTabBarInstance()
if (tabBar && typeof tabBar.syncUnread === 'function') tabBar.syncUnread(force)
}