当前位置:   article > 正文

微信小程序自定义tabbar闪烁问题

微信小程序自定义tabbar闪烁

闪烁问题原因:超过两个tabbar页不要单纯的使用官方说的show时getTabBar().setData设置选中态,自定义tabbar是多个实例的,那样只会改变当前tabbar实例的选中态,其他页面的tabbar实例并没有改变选中态。

解决tabbar闪烁问题:

1.在app.js中设置 globalData

  1. // app.js
  2. App({
  3. globalData: {
  4. tabIndex: 0
  5. },
  6. })

2.在 custom-tab-bar/index.js 中设置

  1. //custom-tab-bar/index.js
  2. const app = getApp()
  3. lifetimes: {
  4. attached() {
  5. this.setData({
  6. selected: app.globalData.tabIndex
  7. })
  8. }
  9. },
  10. methods: {
  11. switchTab(e) {
  12. const data = e.currentTarget.dataset
  13. const url = data.path
  14. wx.switchTab({ url })
  15. app.globalData.tabIndex = data.index
  16. }
  17. },

3.每个tabbar页面设置

  1. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  2. this.getTabBar().setData({
  3. selected: // tabbar的id
  4. })
  5. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/89884
推荐阅读