微信小程序自定义tabbar的实现及遇到的问题

微信小程序自定义tabbar

1. 微信小程序自定义的实现

参考官方文档
https://developers.weixin.qq.com/miniprogram/dev/framework/ability/custom-tabbar.html

  1. 配置信息
    在 app.json 中的 tabBar 项指定 custom 字段,表示使用自定义的tabbar,同时其余 tabBar 相关配置也补充完整。所有 tab 页的 json 里需声明 usingComponents 项,也可以在 app.json 全局开启。
    示例:
  "tabBar": {
    "custom": true,
    "color": "#7A7E83",
    "selectedColor": "#d81e06",
    "borderStyle": "black",
    "backgroundColor": "#ffffff",
    "list": [{
      "pagePath": "pages/home/home",
      "iconPath": "images/home.png",
      "selectedIconPath": "images/home-selected.png",
      "text": "首页"
    }, {
      "pagePath": "pages/mine/mine",
      "iconPath": "images/mine.png",
      "selectedIconPath": "images/mine-selected.png",
      "text": "我的"
    }]
  },
  "usingComponents": {
    "v-tabBar": "custom-tab-bar/index"
  },
  1. 添加 tabBar 代码文件,在代码根目录下添加入口文件:
    custom-tab-bar/index.js
    custom-tab-bar/index.json
    custom-tab-bar/index.wxml
    custom-tab-bar/index.wxss
    此文件可以在官方文档中导入代码片段获得,链接如下(https://developers.weixin.qq.com/s/jiSARvmF7i55)。
    修改index.js里的list为自己的tab列表即可,注意为“/”目录
    贴一下代码如下
    custom-tab-bar/index.js
 Component({
  data: {
    selected: 0,
    "color": "#7A7E83",
    "selectedColor": "#d81e06",
    "list": [{
      "pagePath": "/pages/home/home",
      "iconPath": "/images/home.png",
      "selectedIconPath": "/images/home-selected.png",
      "text": "首页"
    }, {
      "pagePath": "/pages/mine/mine",
      "iconPath": "/images/mine.png",
      "selectedIconPath": "/images/mine-selected.png",
      "text": "我的"
    }]
  },
  attached() {
  },
  methods: {
    switchTab(e) {
      const data = e.currentTarget.dataset
      const url = data.path
      wx.switchTab({url})
      this.setData({
        selected: data.index
      })
    }
  }
})

custom-tab-bar/index.json

{
  "component": true
}

custom-tab-bar/index.wxml

<cover-view class="tab-bar">
  <cover-view class="tab-bar-border"></cover-view>
  <cover-view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
    <cover-image class="aa" src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></cover-image>
    <cover-view class="bb" style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</cover-view>
  </cover-view>
</cover-view>

custom-tab-bar/index.wxss

.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 48px;
  background: white;
  display: flex;
  padding-bottom: env(safe-area-inset-bottom);
}

.tab-bar-border {
  background-color: rgba(0, 0, 0, 0.33);
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 1px;
  transform: scaleY(0.5);
}

.tab-bar-item {
  flex: 1;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

.tab-bar-item .aa {
  width: 27px;
  height: 27px;
}

.tab-bar-item .bb {
  font-size: 10px;
}

  1. 编写 tabBar 代码
    用自定义组件的方式编写即可,该自定义组件完全接管 tabBar 的渲染。另外,自定义组件新增 getTabBar 接口,可获取当前页面下的自定义 tabBar 组件实例。
    在子tab页面的js文件中,把官方给的使用方法放到tabbar跳转页的onShow方法里,selected根据list下标位置进行设置
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
    this.getTabBar()) {
    this.getTabBar().setData({
      selected: 0
    })
  }
  },

2. 遇到的问题

1、模拟器运行结果一直报渲染层错误,如图所示
在这里插入图片描述
后来发现把调试基础库的版本改到了2.6.0就好了。
在这里插入图片描述

2.我实现的是登录后才进入首页和我的页面,一开始跳转用的navigateTo跳转页面custom-tab-bar/index,

 wx.navigateTo({
      url: '../../../custom-tab-bar/index',
      }

但是跳进来首页内容不显示,需要点击tab项按钮才会显示,后来改为switchtab跳转到第一个tab项就好了

 wx.switchTab({
      url: '../../home/home',
    })

成功实现,在这里又把调试基础库改回了2.12.2,并没有报错,说明就是我的子那个定义组件调用的问题,自己想想也是怎么可以跳到自定义组件页,就是从别的页面跳过来一定是到第一个tab项的页面。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值