小程序自定义tabbar双击切换问题汇总

问题描述

小程序自定义的tabbar按照官方的文档使用时,会出现点击无法切换底部tabbar, 双击才能切换的问题,没有去找原因,直接从网上看了一个相对好的方法, 警示后来者……

解决方法
  • 在底部tabbar要跳转的页面的onshow方法内部加入一下代码
 if(typeof this.getTabBar === 'function' && this.getTabBar()) {
    
    
      
      this.getTabBar().setData({
    
    
        selected: 1
      })
    }

selected为要切换的tabbar 的索引

  • 也可以在app.json中封装一下,在要跳转的几个页面 的show方法内调用一下即可
  /**
   * 修复 自定义 tarbar 双击才能切换的bug
   * @param {*} ctx 上下文
   * @param {*} idx 索引
   */
  customTarbarBugRepair(ctx, idx){
    
    
    if(typeof ctx.getTabBar === 'function' && ctx.getTabBar()) {
    
    
      
      ctx.getTabBar().setData({
    
    
        selected: idx
      })
    }
  }

猜你喜欢

转载自blog.csdn.net/ITzhongzi/article/details/114083629