微信小程序-自定义tabBar

通过官网给出的示例自己实现了自定义的tabBar,但结果发现

无法监听页面生命周期函数

结语:原想的是实现不一样的效果(如下)

故尝试了自定义tabBar,虽然做出来了,但也发现这个做法存在不足:

在页面js文件中,需要给出以下代码:

javascript 复制代码
Component({
  pageLifetimes: {
    show() {
      if (typeof this.getTabBar === 'function' &&
        this.getTabBar()) {
        this.getTabBar().setData({
          selected: 0		// 对应tabBar的下标
        })
      }
    }
  },
  data: {
    navBarHeight: getApp().globalData.navBarHeight,
  },
})

相当于将原来的page页面更改为组件components

这样做的结果就是无法监听页面生命周期函数(至少在创建page时自带的生命周期函数均无法监听)

这是我遇到的,若有解决方案,欢迎留言告知

实现步骤

官方文档:基础能力 / 自定义 tabBar (qq.com)

1.app.json配置tabBar

javascript 复制代码
"tabBar": {
    "custom": true,
    "color": "#a5abbd",
    "selectedColor": "#1dba73",
    "backgroundColor": "#ffffff",
    "list": [{
      "pagePath": "pages/home/home",
      "text": "首页"
    },{
      "pagePath": "pages/index/index",
      "text": "测试"
    }]
  }

注意:此处path不写最前面的 /

2.在项目根目录下添加文件夹custom-tab-bar,并添加组件,命名为index(下面的代码均借鉴官方demo,可自行修改)

项目文件夹如下:

index.js:

javascript 复制代码
Component({
  data: {
    selected: 0,
    color: "#a5abbd",
    selectedColor: "#1dba73",
    list: [{
      pagePath: "/pages/home/home",
      iconPath: "/images/icon/home.svg",
      selectedIconPath: "/images/icon/home-selected.svg",
      text: "首页"
    }, {
      // 此处path必须加前面的/,否则在切换tabbar时,会自动拼接地址,导致传入url有误,系统没有任何效果
      pagePath: "/pages/index/index",
      text: "测试"
    }]
  },

  methods: {
    switchTab(e) {
      const data = e.currentTarget.dataset
      const url = data.path
      // wx.switchTab切换tabBar组件,可传入url,success,fail,此处简写
      wx.switchTab({url})
      // 完整写法
      // wx.switchTab({
      //   url: url,
      //   success(res){console.log(res)},
      //   fail(error){console.log(error)}
      // })
      this.setData({
        selected: data.index
      })
    }
  }
})

注意:此处path必须加前面的/,否则在切换tabbar时,会自动拼接地址,导致传入url有误,系统没有任何效果

index.json:

javascript 复制代码
{
  "component": true
}

index.wxml:

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

index.wxss:

css 复制代码
.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 image {
  width: 27px;
  height: 27px;
}

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

到此处应该就会出现tabBar且可点击,点击时也能切换页面

但可能出现问题:一个tabBar要点击两次才能更改样式(即tabBar与页面未连接)

解决:

在页面的js文件中:

javascript 复制代码
Component({
  pageLifetimes: {
    show() {
      if (typeof this.getTabBar === 'function' &&
        this.getTabBar()) {
        this.getTabBar().setData({
          selected: 0		// 对应tabBar的下标
        })
      }
    }
  },
  data: {
    navBarHeight: getApp().globalData.navBarHeight,
  },
})

至此,自定义tabBar便完成了,但就像我前面说的,如此修改之后,无法监听页面的生命周期函数,大家自行判断是否需要自定义tabBar,或者有更好解决方案的,欢迎评论区交流

相关推荐
计算机-秋大田4 小时前
基于微信小程序的电子竞技信息交流平台设计与实现(LW+源码+讲解)
spring boot·后端·微信小程序·小程序·课程设计
计算机徐师兄9 小时前
Java基于SSM框架的互助学习平台小程序【附源码、文档】
小程序·互助学习·互助学习平台小程序·java互助学习微信小程序·互助学习微信小程序·互助学习平台微信小程序
西农小陈10 小时前
Python-基于PyQt5,wordcloud,pillow,numpy,os,sys的智能词云生成器
开发语言·python·小程序·pycharm·numpy·pyqt·pillow
Stanford_110611 小时前
C++中常用的十大排序方法之4——希尔排序
c++·算法·微信小程序·排序算法·微信公众平台·twitter·微信开放平台
说私域12 小时前
开源AI智能名片2+1链动模式S2B2C商城小程序:重塑私域流量运营格局
人工智能·小程序·流量运营
Colinnian15 小时前
微信小程序中在一个大边框里给每个小边框均匀分配空间
微信小程序·小程序·notepad++
一 乐16 小时前
基于微信小程序的酒店管理系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·微信小程序·酒店管理系统
说私域1 天前
今日头条公域流量引流新径:开源 AI 智能名片 2 + 1 链动模式 S2B2C 商城小程序融合之道
人工智能·小程序
曾经的三心草1 天前
小程序-基础加强
小程序·基础加强
说私域1 天前
智能调度体系与自动驾驶技术优化运输配送效率的研究——兼论开源AI智能名片2+1链动模式S2B2C商城小程序的应用潜力
人工智能·小程序·自动驾驶