setCustomTabBar.js 798 Bytes
const TAB_INDEX = {
  '/pages/tab/workspace/workspace': 0,
  '/pages/tab/index/index': 1,
  '/pages/tab/matters/matters': 2,
  '/pages/tab/study/study': 3,
  '/pages/tab/mine/mine': 4,
}

/** 在 tab 页 onShow 中调用,同步自定义 tabBar 选中态 */
function setCustomTabBar(page, index) {
  const apply = () => {
    if (typeof page.getTabBar === 'function' && page.getTabBar()) {
      page.getTabBar().setData({ selected: index })
      return true
    }
    return false
  }
  if (!apply()) {
    wx.nextTick(() => apply())
  }
}

function setCustomTabBarByRoute(page) {
  const route = page.route
  const index = TAB_INDEX[`/${route}`]
  if (index !== undefined) {
    setCustomTabBar(page, index)
  }
}

module.exports = {
  setCustomTabBar,
  setCustomTabBarByRoute,
  TAB_INDEX,
}