当前位置:   article > 正文

uni-app 开发微信小程序 自定义tabBar_uniapp iconpath

uniapp iconpath

pages.js 里面tabBar配置加上"custom": true

  1. "tabBar": {
  2. "custom": true,
  3. "color": "#8a8a8a",
  4. "selectedColor": "#198cfb",
  5. "borderStyle": "black",
  6. "backgroundColor": "#ffffff",
  7. "list": [{
  8. "pagePath": "pages/index/index",
  9. "text": "首页",
  10. "iconPath": "static/tabbar/basics.png",
  11. "selectedIconPath": "static/tabbar/basics_blue.png"
  12. },
  13. {
  14. "pagePath": "pages/UserCenter/index",
  15. "text": "我的",
  16. "iconPath": "static/tabbar/about.png",
  17. "selectedIconPath": "static/tabbar/about_blue.png"
  18. }
  19. ]
  20. }

然后在根目录下新增custom-tab-bar  因为uni-app 不会编译这个文件夹所以要建成微信小程序一样的

 主要的两个文件index.wxml,index.js

  1. <view class="tab-bar-wrapper">
  2. <view class="tab-bar">
  3. <view class="tab-box">
  4. <view class="tab-bar-item" wx:for="{{list}}" wx:key="index" class="tab-bar-item"
  5. data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
  6. <image src="{{selected === index ? item.selectedIconPath : item.iconPath}}" mode=""></image>
  7. <view style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</view>
  8. </view>
  9. </view>
  10. </view>
  11. </view>

index.js

  1. Component({
  2. data: {
  3. selected: 0,
  4. color: "#ffffff",
  5. selectedColor: "#f40918",
  6. list: [{
  7. "pagePath": "/pages/index/index",
  8. "text": "首页",
  9. "iconPath": "/static/tabbar/basics.png",
  10. "selectedIconPath": "/static/tabbar/basics_blue.png"
  11. },{
  12. "pagePath": "/pages/UserCenter/index",
  13. "text": "我的",
  14. "iconPath": "/static/tabbar/about.png",
  15. "selectedIconPath": "/static/tabbar/about_blue.png"
  16. }]
  17. },
  18. attached () {
  19. this.setData({
  20. selected: 0
  21. })
  22. },
  23. observers: {
  24. "selected": function (newVal) {
  25. const index = wx.getStorageSync("tabBarIndex")
  26. if (newVal != index) {
  27. this.setData({
  28. selected: index
  29. })
  30. }
  31. }
  32. },
  33. methods: {
  34. switchTab(e) {
  35. const data = e.currentTarget.dataset
  36. const url = data.path
  37. wx.switchTab({ url })
  38. const index = wx.getStorageSync("tabBarIndex")
  39. this.setData({
  40. selected: index
  41. })
  42. wx.setStorageSync("tabBarIndex", data.index)
  43. }
  44. }
  45. })

在pages/index/index里面的onLoad,onShow方法中 uni.setStorageSync("tabBarIndex", 0) 缓存对应的值

pages/UserCenter/index uni.setStorageSync("tabBarIndex", 1) 

在main.js 中 uni.setStorageSync("tabBarIndex", 0)  这个是在页面初始化的时候将缓存值改为0  不然会显示上一次的缓存值

效果图

样式我没有贴出来 样式就自己改成ui图一样的就好 

 

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

闽ICP备14008679号