当前位置:   article > 正文

优雅的实现微信小程序动态tabBar及组件中实现动态数据_custom": true,

custom": true,

这段时间在开发一个微信小程序,有个需求是,小程序底部的tabBar导航实现动态化。就是根据用户角色不同,显示不同的导航。要实现动态导航首先要考虑的是如何把小程序中的原生导航更改为自定义导航。

一、根据官方文档所说,实现自定义导航首先要在app.js中配置好tabBar项;

  1. "tabBar": {
  2. "custom": true,
  3. "color": "#7A7E83",
  4. "selectedColor": "#2a83f7",
  5. "backgroundColor": "#ffffff",
  6. "list": [{
  7. "iconPath": "images/icon/home.png",
  8. "selectedIconPath": "images/icon/home-a.png",
  9. "pagePath": "pages/index/index",
  10. "text": "首页"
  11. }, {
  12. "iconPath": "images/icon/classify.png",
  13. "selectedIconPath": "images/icon/classify-a.png",
  14. "pagePath": "pages/picSort/index",
  15. "text": "全部分类"
  16. }, {
  17. "iconPath": "images/icon/chat.png",
  18. "selectedIconPath": "images/icon/chat-a.png",
  19. "pagePath": "pages/chat/index",
  20. "text": "聊一聊"
  21. }, {
  22. "iconPath": "images/icon/user.png",
  23. "selectedIconPath": "images/icon/user-a.png",
  24. "pagePath": "pages/myCentre/index",
  25. "text": "我的"
  26. }]
  27. }

注意:"custom": true是重点,默认是没有这个字段的,在配置项中新增即可;

二、在项目的根目录创建“custom-tab-bar”组件;

 三、编写custom-tab-bar组件的导航页面布局、样式表及JS;

index.wxml

  1. <view class="tab-bar">
  2. <view class="tab-bar-item"
  3. wx:for="{{list}}"
  4. wx:key="index"
  5. data-path="{{item.pagePath}}"
  6. data-index="{{index}}"
  7. bindtap="switchTab"
  8. >
  9. <view class="tab-bar-item-img">
  10. <image class="itemImage" src="{{tabIndex === index ? item.selectedIconPath : item.iconPath}}"></image>
  11. </view>
  12. <view class="tab-bar-item-title">
  13. <view class="itemTitle" style="color: {{tabIndex === index ? selectedColor : color}}">{{item.text}}</view>
  14. </view>
  15. </view>
  16. </view>

说明:以上代码中 

 index.wxss

  1. .tab-bar {
  2. position: fixed;
  3. bottom: 0;
  4. left: 0;
  5. right: 0;
  6. height: 96rpx;
  7. background: white;
  8. display: flex;
  9. padding-bottom: env(safe-area-inset-bottom);
  10. border-top: 2rpx solid #dddddd;
  11. }
  12. .tab-bar .tab-bar-item {
  13. flex: 1;
  14. text-align: center;
  15. display: flex;
  16. justify-content: center;
  17. align-items: center;
  18. flex-direction: column;
  19. height: 100%;
  20. }
  21. .tab-bar .tab-bar-item .tab-bar-item-img .itemImage {
  22. width: 54rpx;
  23. height: 54rpx;
  24. }
  25. .tab-bar .tab-bar-item .tab-bar-item-title .itemTitle {
  26. font-size: 20rpx;
  27. }

index.js 

  1. Component({
  2. data: {
  3. tabIndex: 0
  4. custom: true,
  5. color: "#7A7E83",
  6. selectedColor: "#2a83f7",
  7. backgroundColor: "#ffffff",
  8. list: [{
  9. iconPath: "images/icon/home.png",
  10. selectedIconPath: "images/icon/home-a.png",
  11. pagePath: "pages/index/index",
  12. text: "首页"
  13. }, {
  14. iconPath: "images/icon/classify.png",
  15. selectedIconPath: "images/icon/classify-a.png",
  16. pagePath: "pages/picSort/index",
  17. text: "全部分类"
  18. }, {
  19. iconPath: "images/icon/chat.png",
  20. selectedIconPath: "images/icon/chat-a.png",
  21. pagePath: "pages/chat/index",
  22. text: "聊一聊"
  23. }, {
  24. iconPath: "images/icon/user.png",
  25. selectedIconPath: "images/icon/user-a.png",
  26. pagePath: "pages/myCentre/index",
  27. text: "我的"
  28. }]
  29. },
  30. methods: {
  31. switchTab(e) {
  32. const data = e.currentTarget.dataset
  33. // console.log('currentTarget:', data.index)
  34. const url = data.path
  35. wx.switchTab({url})
  36. this.setData({
  37. selected: data.index
  38. })
  39. }
  40. }
  41. })

重点:在实现自定义tabbar的时候,会出现点击导航,高亮状态及跳转链接出现混乱的现象, 这是因为使用Component组件是不允许设置动态数据的。下面会一并解决如何解决这些问题。

四、上面部分不是重点,在任何地方都能搜索。而实现动态 tabbar是写这边文章的重点

 1、我们先在utils目录中创建tab-service.js文件,写上全局的数据及方法;

  1. // Tab页的data
  2. let tabData = {
  3. tabIndex: 0,
  4. tabBar: {
  5. showplate: '',
  6. custom: true,
  7. color: "#7A7E83",
  8. selectedColor: "#2a83f7",
  9. backgroundColor: "#ffffff",
  10. list: [{
  11. iconPath: "/images/icon/home.png",
  12. selectedIconPath: "/images/icon/home-a.png",
  13. pagePath: "/pages/index/index",
  14. text: "首页"
  15. }, {
  16. iconPath: "/images/icon/classify.png",
  17. selectedIconPath: "/images/icon/classify-a.png",
  18. pagePath: "/pages/picSort/index",
  19. text: "全部分类"
  20. }, {
  21. iconPath: "/images/icon/chat.png",
  22. selectedIconPath: "/images/icon/chat-a.png",
  23. pagePath: "/pages/chat/index",
  24. text: "聊一聊"
  25. }, {
  26. iconPath: "/images/icon/user.png",
  27. selectedIconPath: "/images/icon/user-a.png",
  28. pagePath: "/pages/myCentre/index",
  29. text: "我的"
  30. }]
  31. }
  32. }
  33. // 更新菜单
  34. const updateRole = (that, type) => {
  35. if (type === '0') {
  36. tabData.tabBar.list.forEach((item, i) => {
  37. if (item.text === '我的') {
  38. tabData.tabBar.list.splice(i, 1)
  39. }
  40. })
  41. } else if (type === '1') {
  42. const num = tabData.tabBar.list.length
  43. if (num === 3) {
  44. tabData.tabBar.list.push({
  45. iconPath: "/images/icon/user.png",
  46. selectedIconPath: "/images/icon/user-a.png",
  47. pagePath: "/pages/myCentre/index",
  48. text: "我的"
  49. })
  50. }
  51. }
  52. // console.log('tabData:', tabData)
  53. updateTab(that);
  54. }
  55. // 更新底部高亮
  56. const updateIndex = (that, index) => {
  57. tabData.tabIndex = index;
  58. updateTab(that);
  59. }
  60. // 更新Tab状态
  61. const updateTab = (that) => {
  62. if (typeof that.getTabBar === 'function' && that.getTabBar()) {
  63. that.getTabBar().setData(tabData);
  64. }
  65. }
  66. // 将可调用的方法抛出让外面调用
  67. module.exports = {
  68. updateRole, updateTab, updateIndex
  69. }

2、把custom-tab-bar目录中的index.wxml修改为下面这段

  1. <view class="tab-bar">
  2. <view class="tab-bar-item"
  3. wx:for="{{tabBar.list}}"
  4. wx:key="index"
  5. data-path="{{item.pagePath}}"
  6. data-index="{{index}}"
  7. bindtap="switchTab"
  8. >
  9. <view class="tab-bar-item-img">
  10. <image class="itemImage" src="{{tabIndex === index ? item.selectedIconPath : item.iconPath}}"></image>
  11. </view>
  12. <view class="tab-bar-item-title">
  13. <view class="itemTitle" style="color: {{tabIndex === index ? tabBar.selectedColor : tabBar.color}}">{{item.text}}</view>
  14. </view>
  15. </view>
  16. </view>

其中tabBar就是在tab-service.js文件中写的公共数据。

2、把index.js修改为下面这段

  1. Component({
  2. data: {
  3. },
  4. methods: {
  5. switchTab(e) {
  6. const data = e.currentTarget.dataset
  7. // console.log('currentTarget:', data.index)
  8. const url = data.path
  9. wx.switchTab({url})
  10. this.setData({
  11. selected: data.index
  12. })
  13. }
  14. }
  15. })

3、重点是在对应导航的JS文件中找到onShow方法,写上tabService.updateIndex(this, 0),其中tabService就是我们写的全局tab-service.js,引入一下

const tabService = require("../../utils/tab-service")

在js,onShow方法写上tabService.updateIndex(this, 0),这是因为需要调用一次刷新事件。如果导航有4个,每个导航对一个页面,0,1,2,3以此类推即可;

  1. onShow: function () {
  2. tabService.updateIndex(this, 0)
  3. }

4、 因为我要实现页面加载时判断用户,根据用户的不同改变导航的。所以我在onLand方法中去判断调用tabService.updateRole(this, res.data.showplate)这个公共的方法;

这样就完全解决了动态tabbar的效果;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/101602
推荐阅读
相关标签
  

闽ICP备14008679号