当前位置:   article > 正文

uniapp自定义tabbar_uniapp 自定义tabbar

uniapp 自定义tabbar

在开发微信小程序时,由于uniapp官方的tabbar不满足根据权限区分tab的要求,则自定义了tabbar,出现两个问题;

问题一:自定义的tabbar在切换的时候tabbar会闪烁,尤其在用户手机性能不太好的手机型号上闪烁尤其明显;

问题二:自定义的tabbar使用uni.redirectTo切换页面每次都会触发页面的onLoad,页面数据得不到缓存,消耗性能。

所以将官方的tabbar和自定义的tabbar结合使用,

(1)在tabbar页面使用uni.hideTabBar({})将官方的tabbar页面隐藏

(2)封装自定义tabbar组件,根据权限分配tab

1. 在pages.json中把tabbar配置好,只需要配置页面路径即可

  1. "tabBar": {
  2. "color": "#909399",
  3. "selectedColor": "#303133",
  4. "backgroundColor": "#FFFFFF",
  5. "borderStyle": "black",
  6. "list": [
  7. {
  8. "pagePath": "pages/index/index",
  9. "text":""
  10. },
  11. {
  12. "pagePath": "pages/workOrder/workOrderList",
  13. "text":""
  14. },
  15. {
  16. "pagePath": "pages/my/my",
  17. "text":""
  18. }
  19. ]
  20. },

2. 封装tabbar组件,tabbar.vue,使用uni.switchTab({})路由切换tabBar页面

  1. <template>
  2. <cover-view class="tabbar" :style="{'padding-bottom': paddingBottomHeight + 'rpx'}">
  3. <cover-view
  4. class="tabbar-item"
  5. v-for="(item,index) in tabNav"
  6. :key="index"
  7. @click="tabbarChange(item.pagePath,index)"
  8. >
  9. <cover-image class="item-img" :src="item.selectedIconPath" v-if="current == index"></cover-image>
  10. <cover-image class="item-img" :src="item.iconPath" v-else></cover-image>
  11. <cover-view
  12. class="item-text"
  13. :class="{'tabbarActive': current == index}"
  14. style="color: #A3A3A3;font-size: 20rpx"
  15. v-if="item.text">{{item.text}}</cover-view>
  16. </cover-view>
  17. </cover-view>
  18. </template>
  19. <script>
  20. export default {
  21. props: {
  22. current: String,
  23. },
  24. data() {
  25. return {
  26. tabNav: [],
  27. paddingBottomHeight: 0 //iPhonex以上手机底部适配高度
  28. }
  29. },
  30. created() {
  31. //适配iPhonex以上的底部
  32. uni.getSystemInfo({
  33. success: (res) => {
  34. let model = ['X', 'XR', 'XS', '11', '12', '13', '14', '15'];
  35. // console.log('iphone',res)
  36. for (let i=0;i<=model.length;i++) {
  37. if(res.model.indexOf(model[i]) != -1 && res.model.indexOf('iPhone') != -1) {
  38. this.paddingBottomHeight = 50
  39. //向父组件传递tabbar高度
  40. this.$emit('tabbarHeight',150)
  41. return
  42. }else {
  43. this.$emit('tabbarHeight',100)
  44. }
  45. }
  46. }
  47. })
  48. // 根据权限配置对应tab导航
  49. let list1 = [{
  50. pagePath: "/pages/index/index",
  51. iconPath: "/static/nav/home.png",
  52. selectedIconPath: "/static/nav/home_select.png",
  53. text: "首页"
  54. },{
  55. pagePath: "/pages/workOrder/workOrderList",
  56. iconPath: "/static/nav/work_order.png",
  57. selectedIconPath: "/static/nav/work_order_select.png",
  58. text: "工单"
  59. },{
  60. pagePath: "/pages/my/my",
  61. iconPath: "/static/nav/my.png",
  62. selectedIconPath: "/static/nav/my_select.png",
  63. text: "我的"
  64. },]
  65. let list2 = [{
  66. pagePath: "/pages/index/index",
  67. iconPath: "/static/nav/home.png",
  68. selectedIconPath: "/static/nav/home_select.png",
  69. text: "首页"
  70. },{
  71. pagePath: "/pages/my/my",
  72. iconPath: "/static/nav/my.png",
  73. selectedIconPath: "/static/nav/my_select.png",
  74. text: "我的"
  75. },]
  76. if(this.$store.state.role == '1') {
  77. this.tabNav = list1
  78. }else {
  79. this.tabNav = list2
  80. }
  81. },
  82. methods: {
  83. //跳转切换tab
  84. tabbarChange(path) {
  85. uni.switchTab({
  86. url: path
  87. })
  88. }
  89. }
  90. }
  91. </script>
  92. <style scoped lang="scss">
  93. .tabbarActive{
  94. color: #000 !important;
  95. }
  96. .tabbar{
  97. position: fixed;
  98. bottom: 0;
  99. left: 0;
  100. display: flex;
  101. justify-content: space-around;
  102. align-items: center;
  103. width: 100%;
  104. height: 100rpx;
  105. background-color: #ffffff;
  106. border-top: 1px solid #e5eaea;
  107. }
  108. .tabbar-item{
  109. flex: 1;
  110. display: flex;
  111. flex-direction: column;
  112. align-items: center;
  113. justify-content: center;
  114. // height: 100rpx;
  115. }
  116. .item-img{
  117. margin-bottom: 4rpx;
  118. width: 46rpx;
  119. height: 46rpx;
  120. }
  121. .item-name{
  122. font-size: 26rpx !important;
  123. color: #A3A3A3 !important;
  124. }
  125. </style>

3. 在tabbar页面引入组件,隐藏官方tabbar,

  1. onShow() {
  2. uni.hideTabBar({
  3. animation: false
  4. })
  5. },

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

闽ICP备14008679号