微信小程序自定义tabBar的图标选中问题

微信小程序自定义tabBar时图标选中问题

在使用自定义tabBar时要在目标目录的onShow函数中写入一段判断代码,否则图标的选中就会发生异常
自定义tabBar参考官方文档https://developers.weixin.qq.com/miniprogram/dev/framework/ability/custom-tabbar.html
在custom-tab-bar目录中js文件:

Component({
  data: {
    active: 0,
    list:[
      {
        path:"/pages/index1/index1",
        icon:"home-o",
        text:"首页"
      },
      {
        path: "/pages/chat/chat",
        icon: "apps-o",
        text: "分类"
      },
      {
        path: "/pages/mine/mine",
        icon: "setting-o",
        text: "我的"
      }
    ]
  },
  methods: {
    switchTab(e) {
      const data = e.currentTarget.dataset
      const url = data.path
      wx.switchTab({url})
      this.setData({
        active: data.index
      })
      console.log(this)
    }
  }
})

则要在上述中的path:
/pages/index1/index1
/pages/chat/chat
/pages/mine/mine
的js文件中的onShow函数中加入以下代码条件判断图标是否选中

onShow: function () {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        active: 1  //这个数字是当前页面在tabBar中list数组的索引
      })
    }
  },

加入后图标选中正常
自定义tabBar

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值