微信小程序---自定义底部导航栏

实现:不同身份登录小程序显示不同的底部导航栏。

比如,一个微信小程序需要两个版本:用户版、商家版。

在全局配置app.json中配置tabBar信息

  • custom:设为 true,表示使用自定义组件
  • list:tab 页列表,在列表中的页面将被设置为 tab 页,自动加载 tabBar

        app.json代码如下: 

{
  "pages":[
    XXX
  ],
  "window":{
    XXX
  },
  "tabBar": {
    "custom": true,
    "color": "#7A7E83", //tab上的文字默认颜色
    "selectedColor": "#d81e06", //tab上的文字选中时的颜色
    "borderStyle": "black", //tabbar上边框的颜色,仅支持 black/white
    "backgroundColor": "#ffffff", //tab的背景色
    "list":[
      {
        "text": "首页",
        "pagePath": "pages/index/index",
        "iconPath": "/images/01.png",
        "selectedIconPath": "/images/01_select.png"
      },
      {
        "text": "校园",
        "pagePath": "pages/campus/campus",
        "iconPath": "/images/02.png",
        "selectedIconPath": "/images/02_select.png"
      },
      {
        "text": "我的",
        "pagePath": "pages/person/person",
        "iconPath": "/images/03.png",
        "selectedIconPath": "/images/03_select.png"
      },
      {
        "text": "订单",
        "pagePath": "pages/order/order",
        "iconPath": "/images/04.png",
        "selectedIconPath": "/images/04_select.png"
      },
      {
        "text": "个人",
        "pagePath": "pages/person/trader",
        "iconPath": "/images/03.png",
        "selectedIconPath": "/images/03_select.png"
      }
    ]
  },
  "style": "v2",
}

 添加tabBar代码文件

      创建一个与 /pages 的同级目录,命名为 custom-tab-bar,然后在目录中创建组件 index

注意:

  1. 目录层级与目录命名问题,不可用其他名称命名。
  2. 是在文件的根目录下创建组件。

 创建成功后,文件的目录如下图所示:

编写tabBar代码 

 index.js 代码如下:

// custom-tab-bar/index.js
Component({
    /**组件的属性列表*/
    properties: {
    },

    /**组件的初始数据*/
    data: {
      selected: 0, //属性默认值
      color: "#000000",
      roleId: 0, //roleId=0时,表示为“顾客”;roleId=1时,表示为“商家”。
      selectedColor: "#1396DB",
      allList: [{
          list1: [ //顾客底部导航栏
              {
                  "text": "首页",
                  "pagePath": "/pages/index/index",
                  "iconPath": "/images/01.png",
                  "selectedIconPath": "/images/01_select.png"
              },
              {
              "text": "校园",
              "pagePath": "/pages/campus/campus",
              "iconPath": "/images/02.png",
              "selectedIconPath": "/images/02_select.png"
              },
              {
              "text": "我的",
              "pagePath": "/pages/person/person",
              "iconPath": "/images/03.png",
              "selectedIconPath": "/images/03_select.png"
              }
          ],
          list2: [ //商家底部导航栏
              {
                  "text": "订单",
                  "pagePath": "/pages/order/order",
                  "iconPath": "/images/04.png",
                  "selectedIconPath": "/images/04_select.png"
              },
              {
              "text": "个人",
              "pagePath": "/pages/person/trader",
              "iconPath": "/images/03.png",
              "selectedIconPath": "/images/03_select.png"
              }
          ]
      }],
      list: []
    },

    attached() {
        const roleId = wx.getStorageSync('status') //获取缓存roleId
        if (roleId == 0) { //顾客
          this.setData({
            list: this.data.allList[0].list1,
          })
        }else if(roleId == 1) { //商家
          this.setData({
            list: this.data.allList[0].list2
          })
        }
        wx.setStorageSync('list', this.data.list) //存入缓存
    },

    /** 组件的方法列表*/
    methods: {
        switchTab(e) { //根据不同的路径切换tabbar下标
            // console.log('组件里面的数据',e)
            const data = e.currentTarget.dataset
            const path = data.path
            this.setData({
              selected: data.index
            })
            wx.switchTab({ 
              url: path,
            })
            // console.log('组件:', this.data.selected)
        }
    }
})

index.json 代码如下:

{
    "component": true,
    "usingComponents": {}
}

index.wxml 代码如下:

<!--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="cover-image" src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></cover-image>
    <cover-view class="cover-view" style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</cover-view>
  </cover-view>
</cover-view>

index.wxss 代码如下:

/* 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 .cover-image {
    width: 50rpx;
    height: 50rpx;
}
.tab-bar-item .cover-view {
    margin-top: 6rpx;
    font-size: 24rpx;
} 

注意,还有在具体的tabBar页面里添加如下代码:

/** 生命周期函数--监听页面显示*/
 onShow: function () {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        // 当前页面的 tabBar 索引
        selected: 0
      })
    }
  },

实际效果

底部导航栏

 

参考资料:微信小程序自定义 tabBar 踩坑实践 - 掘金 (juejin.cn)



不需要在 app.json 的 usingComponents 引入 tabBar 组件,如果你放置目录与命名正确,小程序会自动引入。

小问题:如果出现“ Component is not found in path "custom-tab-bar/index"  或者 getTabBar() 始终返回 null,那么很可能就是tabBar组件目录放置和文件命名问题。

【本博客记载了本人在实践过程中遇到的一些问题以及解决方法,希望能够帮助到大家!】

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值