小程序自定义tab-bar,踩坑记录

从官方下载代码

https://developers.weixin.qq.com/miniprogram/dev/framework/ability/custom-tabbar.html

1、把custom-tab-bar 文件放置 pages同级

修改下 custom-tab-bar 下的 JS文件

c 复制代码
Component({
  data: {
    selected: 0,
    color: "#7A7E83",
    selectedColor: "#3cc51f",
    list: [{
      pagePath: "/pages/index/index",
      iconPath: "/static/images/ico/home.png",
      selectedIconPath: "/static/images/ico/home.png",
      text: "A"
    }, {
      pagePath: "/pages/product/product",
      iconPath: "/static/images/ico/home.png",
      selectedIconPath: "/static/images/ico/home.png",
      text: "B"
    },
    {
        pagePath: "/pages/news/news",
        iconPath: "/static/images/ico/home.png",
        selectedIconPath: "/static/images/ico/home.png",
        text: "C"
      },
      {
        pagePath: "/pages/my/my",
        iconPath: "/static/images/ico/home.png",
        selectedIconPath: "/static/images/ico/home.png",
        text: "D"
      },
]
  },
  attached() {
  },
  methods: {
    switchTab(e) {
      const data = e.currentTarget.dataset
      const url = data.path
      wx.switchTab({url})
    //   this.setData({  这部分注销,到其他页进行赋值
    //     selected: data.index
    //   })
    }
  }
})

2、跳转到指定页面时,在当页面JS上加上代码:

c 复制代码
onShow: function () {  //上面注销得部分, 在A-D页面对应上,  A页面=0 ,B=1 以此类推
    if (typeof this.getTabBar === 'function' &&  this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0     //这里的数字设置当前页面在tabbar里对应的序列,从0开始
      })
    }
  }

3、在app.json 开启自定义tab

c 复制代码
  "tabBar": {
    "custom": true,  //开启
    "color": "#767676",
    "selectedColor": "#004F8A",
    "backgroundColor": "#fff",
    "borderStyle": "black",
    "list": [

4、在app.js 隐藏原生得JS

c 复制代码
// app.js
App({
  onLaunch() {
      wx.hideTabBar(); //隐藏原生得tab bar 
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
相关推荐
2501_915918413 小时前
使用 HBuilder 上架 iOS 应用时常见的问题与应对方式
android·ios·小程序·https·uni-app·iphone·webview
2501_916007475 小时前
iOS 崩溃日志的分析方法,将崩溃日志与运行过程结合分析
android·ios·小程序·https·uni-app·iphone·webview
2501_916007475 小时前
React Native 混淆在真项目中的方式,当 JS 和原生同时暴露
javascript·react native·react.js·ios·小程序·uni-app·iphone
00后程序员张6 小时前
苹果应用商店上架App流程,签名证书、IPA 校验、上传
android·ios·小程序·https·uni-app·iphone·webview
2501_916007476 小时前
iOS 上架需要哪些准备,围绕证书、描述文件和上传方式等关键环节展开分析
android·ios·小程序·https·uni-app·iphone·webview
qq_12498707536 小时前
基于微信小程序的私房菜定制上门服务系统(源码+论文+部署+安装)
java·spring boot·微信小程序·小程序·毕业设计·毕设
2501_915106326 小时前
iOS 上架费用解析,哪些成本可以通过流程优化降低。
android·ios·小程序·https·uni-app·iphone·webview
风月歌7 小时前
小程序项目之超市售货管理平台小程序源代码(源码+文档)
java·微信小程序·小程序·毕业设计·源码
Lily.C8 小时前
小程序WebSocket实时通信全解析
websocket·网络协议·小程序
风月歌8 小时前
基于小程序的超市购物系统设计与实现源码(java+小程序+mysql+vue+文档)
java·mysql·微信小程序·小程序·毕业设计·源码