收藏
回答

怎样解决Cannot read property 'setData' of null问题?

Page({
  onShow: function () {
    this.getTabBar().setData({selectedIndex:0});  
}
})

看上面的代码,我刚才试了一下,结果出现了Cannot read property 'setData' of null的错误(如下图),有没有更好的解决方法解决这个错误?

回答关注问题邀请回答
收藏

1 个回答

  • 云阣
    云阣
    01-29
    if (typeof this.getTabBar === 'function' ) {
        this.getTabBar((tabBar) => {
          tabBar.setData({
            selected: 0
          })
        })
      }
    
    01-29
    有用
    回复 15
    • 我是谢明浩
      我是谢明浩
      01-29
      尝试了,又报错了
      页面【pages/index/index]错误:
       TypeError: Cannot read property 'getTabBar' of undefined
          at index.js? [sm]:2
          at WASubContext.js?t=wechat&s=1706507854346&v=3.3.3:1
          at p.runWith (WASubContext.js?t=wechat&s=1706507854346&v=3.3.3:1)
          at q (WASubContext.js?t=wechat&s=1706507854346&v=3.3.3:1)
          at <anonymous>:8:5
          at evaluateRes (index.js:1)
          at index.js:1
          at Object.invoke (index.js:1)
          at r (index.js:1)
          at Object.invoke (WAServiceMainContext.js?t=wechat&s=1706507854346&v=3.3.3:1)(env: Windows,mp,1.06.2401020; lib: 3.3.3)
      TypeError: Cannot read property 'getTabBar' of undefined
          at index.js? [sm]:2
          at WASubContext.js?t=wechat&s=1706507854346&v=3.3.3:1
          at p.runWith (WASubContext.js?t=wechat&s=1706507854346&v=3.3.3:1)
          at q (WASubContext.js?t=wechat&s=1706507854346&v=3.3.3:1)
          at <anonymous>:8:5
          at evaluateRes (index.js:1)
          at index.js:1
          at Object.invoke (index.js:1)
          at r (index.js:1)
          at Object.invoke (WAServiceMainContext.js?t=wechat&s=1706507854346&v=3.3.3:1)(env: Windows,mp,1.06.2401020; lib: 3.3.3)
      01-29
      回复
    • 云阣
      云阣
      01-29回复我是谢明浩
      自定义的tabbar吗
      01-29
      回复
    • 我是谢明浩
      我是谢明浩
      01-29回复云阣
      是的,我需要制作的是底部的多标签栏
      01-29
      回复
    • 云阣
      云阣
      01-29回复我是谢明浩
      有简单的代码片段吗,我这边测试没问题
      01-29
      回复
    • 我是谢明浩
      我是谢明浩
      01-29回复云阣
      代码比较多,例如
      01-29
      回复
    查看更多(10)
登录 后发表内容