收藏
回答

自定义底部tabbar点击某一个后没有显示相应状态

根据官方demo写的自定义底部tabbar,当在点击其中一个的时候没切换相应的状态,如图默认状态


当点击关注的时候路径变了,但是没有显示没有切换到关注


代码如下:

<cover-view class="tab-bar">
  <cover-view class="tab-bar-border" style="background-color:{{borderColor}}"></cover-view>
  <cover-view class="progress" style="width:{{progress}};background-color:{{progressColor}}"></cover-view>
  <block wx:for="{{list}}" wx:key="index">
    <cover-view class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
        <cover-view style="color: {{selected == index ? selectedColor : color}}" wx:if="{{item.text}}">{{item.text}}</cover-view>
        <cover-image src="{{selected == index ? item.selectedIconPath : item.iconPath}}" class="single-img" wx:else></cover-image>
      </cover-view>
  </block>
</cover-view>
Component({
  data: {
    selected: 0,
    color: "#7A7E83",
    selectedColor: "#fff",
    borderColor: "rgba(0, 0, 0, 0.33)",
    progress: '80%',
    progressColor: "#f40",
    list: [{
      pagePath: "/index/index/index",
      iconPath: "/image/icon_component.png",
      selectedIconPath: "/image/icon_component_HL.png",
      text: "首页"
    }, {
      pagePath: "/index/follow/follow",
      iconPath: "/image/icon_API.png",
      selectedIconPath: "/image/icon_API_HL.png",
      text: "关注"
    }, {
      pagePath: "/index/record/record",
      iconPath: "/image/icon_API.png",
      selectedIconPath: "/image/icon_API_HL.png",
      text: ""
    }]
  },
  attached() {},
  methods: {
    switchTab(e) {
      const data = e.currentTarget.dataset
      const url = data.path
      wx.switchTab({
        url
      })
      this.setData({
        selected: data.index
      })
    }
  }
})


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

1 个回答

  • 轩辕狗剩
    轩辕狗剩
    2019-04-08

    在引用tabbar的每个页面的onshow里,增加

    if (typeof this.getTabBar === 'function' && this.getTabBar()) {      this.getTabBar().setData({        selected: 0      })  }

    selected的值是该页面的索引

    2019-04-08
    有用 1
    回复 2
    • 暗影之ZERO ᯤ⁶ᴳ
      暗影之ZERO ᯤ⁶ᴳ
      2020-09-16
      有用,有用,太有用了
      2020-09-16
      回复
    • Koiiiiiii
      Koiiiiiii
      2022-08-26
      请问一下,我也加了这些代码了,为什么还是没有作用
      2022-08-26
      回复
登录 后发表内容