当前位置: 首页 > news >正文

微信小程序-自定义tabBar

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

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

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

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

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

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

"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:

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.datasetconst 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:

{"component": true
}

index.wxml:

<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:

.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文件中:

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

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

http://www.lryc.cn/news/406867.html

相关文章:

  • vue3+element-plus 实现动态菜单和动态路由的渲染
  • GO-学习-03-基本数据类型
  • 高并发场景下,系统的保护机制
  • 服务器构建私有npm库(Docker + Verdaccio)
  • LabVIEW做二次开发时应该注意哪些方面?
  • docker配置上网代理获取镜像
  • SqlSugar删除没有定义主键的实体类对应的数据库表数据
  • 虚拟机复制后网络不可用,报错“network.service - LSB: Bring up/down networking”
  • Redis 7.x 系列【30】集群管理命令
  • 将YOLOv8模型从PyTorch的.pt格式转换为TensorRT的.engine格式
  • Hello SLAM(在Linux中实现第一个C++程序)
  • IPD推行成功的核心要素(十五)项目管理提升IPD相关项目交付效率和用户体验
  • C++ 鼠标轨迹API【神诺科技SDK】
  • 设计模式|观察者模式
  • python自动化运维 通过paramiko库和time库实现服务器自动化管理
  • HTML常用的转义字符——怎么在网页中写“<div></div>”?
  • shell-awk文本处理工具
  • 如何在测试中保护用户隐私!
  • ARCGIS PRO DSK GraphicsLayer创建文本要素
  • 看板项目之vue代码分析
  • lua 游戏架构 之 游戏 AI (七)ai_dead
  • 前端开发知识(一)-html
  • 身份证如何查验真伪?C#身份证二要素、三要素接口集成
  • C++ | Leetcode C++题解之第290题单词规律
  • Pytorch使用教学7-张量的广播
  • 生成式AI:对话系统(Chat)与自主代理(Agent)的和谐共舞
  • 唯众物联网(IOT)全功能综合实训教学解决方案
  • 24证券从业考试报名『个人信息表』填写模板❗
  • 深度学习系列70:模型部署torchserve
  • 算法日记day 20(中序后序遍历序列构造二叉树|最大、合并、搜索二叉树)