当前位置:   article > 正文

uni-app根据权限显示不同的tabBar_uniapp小程序不同角色登录展示不同的tabbar

uniapp小程序不同角色登录展示不同的tabbar
1、将pages.json中的tabBarlist设置为空数组,即不再使用默认系统自带的tabBar组件。
2、创建tabBar.vue组件,组件内的代码内容如下。
  1. <template>
  2. <view class="tab-bar">
  3. <view v-for="(item,index) in list" :key="index" class="tab-bar-item" @click="switchTab(item, index)">
  4. <image class="tab_img" :src="currentIndex == index ? item.selectedIconPath : item.iconPath"></image>
  5. <view class="tab_text" :style="{color: currentIndex == index ? selectedColor : color}">{{item.text}}</view>
  6. </view>
  7. </view>
  8. </template>
  9. <script>
  10. export default {
  11. props: {
  12. selectedIndex: { // 当前选中的tab index
  13. default: 0
  14. },
  15. },
  16. data() {
  17. return {
  18. color: "#666666",
  19. selectedColor: "#f0c786",
  20. list: [{
  21. "pagePath": "/pages/index/home/index",
  22. "text": "首页",
  23. "iconPath": "/static/tabbar/tab_home.png",
  24. "selectedIconPath": "/static/tabbar/tab_home_sel.png"
  25. }, {
  26. "pagePath": "/pages/index/type",
  27. "text": "货品",
  28. "iconPath": "/static/tabbar/tab_type.png",
  29. "selectedIconPath": "/static/tabbar/tab_type_sel.png"
  30. }, {
  31. "pagePath": "/pages/index/myself/index",
  32. "text": "我的",
  33. "iconPath": "/static/tabbar/tab_my.png",
  34. "selectedIconPath": "/static/tabbar/tab_my_sel.png"
  35. }],
  36. currentIndex: 0,
  37. }
  38. },
  39. created() {
  40. this.currentIndex = this.selectedIndex;
  41. if (uni.getStorageSync('login7') == 1) {
  42. this.list.unshift({
  43. "pagePath": "/pages/index/cart",
  44. "text": "收银",
  45. "iconPath": "/static/tabbar/tab_cart.png",
  46. "selectedIconPath": "/static/tabbar/tab_cart_sel.png"
  47. }, {
  48. "pagePath": "/pages/index/vip",
  49. "text": "会员",
  50. "iconPath": "/static/tabbar/tab_vip.png",
  51. "selectedIconPath": "/static/tabbar/tab_vip_sel.png"
  52. })
  53. } else {
  54. this.list = [{
  55. "pagePath": "/pages/index/home/index",
  56. "text": "首页",
  57. "iconPath": "/static/tabbar/tab_home.png",
  58. "selectedIconPath": "/static/tabbar/tab_home_sel.png"
  59. }, {
  60. "pagePath": "/pages/index/type",
  61. "text": "货品",
  62. "iconPath": "/static/tabbar/tab_type.png",
  63. "selectedIconPath": "/static/tabbar/tab_type_sel.png"
  64. }, {
  65. "pagePath": "/pages/index/myself/index",
  66. "text": "我的",
  67. "iconPath": "/static/tabbar/tab_my.png",
  68. "selectedIconPath": "/static/tabbar/tab_my_sel.png"
  69. }]
  70. }
  71. },
  72. methods: {
  73. switchTab(item, index) {
  74. this.currentIndex = index;
  75. let url = item.pagePath;
  76. uni.redirectTo({
  77. url: url
  78. })
  79. }
  80. }
  81. }
  82. </script>
  83. <style lang="scss">
  84. .tab-bar {
  85. position: fixed;
  86. bottom: 0;
  87. left: 0;
  88. right: 0;
  89. height: 100rpx;
  90. background: white;
  91. display: flex;
  92. justify-content: center;
  93. align-items: center;
  94. padding-bottom: env(safe-area-inset-bottom); // 适配iphoneX的底部
  95. z-index: 999;
  96. .tab-bar-item {
  97. flex: 1;
  98. text-align: center;
  99. display: flex;
  100. justify-content: center;
  101. align-items: center;
  102. flex-direction: column;
  103. .tab_img {
  104. width: 37rpx;
  105. height: 41rpx;
  106. }
  107. .tab_text {
  108. font-size: 20rpx;
  109. margin-top: 9rpx;
  110. }
  111. }
  112. }
  113. </style>
3、在main.js文件中将自定义的tabBar定义为全局组件。
  1. import tabBar from "components/tabBar/tabBar.vue"
  2. Vue.component('tabBar',tabBar)
4、在每一个原本将要设置为tabBar的子页面添加我们自定义的tabBar组件。
5、登录页面中成功登录以后根据相关字段控制切换相关逻辑。
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/103233
推荐阅读
相关标签