当前位置:   article > 正文

uni-app 中两个系统各自显示不同的tabBar_uni 不同的tabbar

uni 不同的tabbar

最近在一个uni-app项目中遇到一个需求,在登录页面成功登录以后需要判断身份,不同的身份的进入不同的tabBar页面,但是在uni-app项目中pages.json中的tabBarlist数组只有一个,且不能写成动态的,那如何实现这个需求呢?答案是需要我们自定义tabBar

目录

1、我们确定在 pages.json文件中的pages数组中的第一个页面就是进入程序时展示的第一个页面,那这个页面肯定就是我们的登录页面了。

2、将pages.json中的tabBar的list设置为空数组,即不再使用默认系统自带的tabBar组件。

3、创建tabBar.vue组件,组件内的代码内容如下。

4、在main.js文件中将自定义的tabBar定义为全局组件。

5、在每一个原本将要设置为tabBar的子页面添加我们自定义的tabBar组件。

6、创建一个新的页面来进行对不同系统进行操作

7.设置完后需要在每个tabbar页面中配置css  


1、我们确定在 pages.json文件中的pages数组中的第一个页面就是进入程序时展示的第一个页面,那这个页面肯定就是我们的登录页面了。
2、将pages.json中的tabBarlist设置为空数组,即不再使用默认系统自带的tabBar组件。
3、创建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: {
  13. // 当前选中的tab index
  14. default: 0,
  15. },
  16. },
  17. data() {
  18. return {
  19. color: '#666666',
  20. selectedColor: '#00BAB2',
  21. list: [],
  22. currentIndex: 0,
  23. };
  24. },
  25. created() {
  26. this.currentIndex = this.selectedIndex;
  27. var _this = this;
  28. if (uni.getStorageSync('system') == 'diagnosis') {
  29. //故障诊断系统
  30. _this.list = [
  31. {
  32. pagePath: '/pages/terbineList/index',
  33. iconPath: '/static/images/tabbar/terbine.png',
  34. selectedIconPath: '/static/images/tabbar/terbine_select.png',
  35. // "text": "风机列表"
  36. },
  37. {
  38. pagePath: '/pages/warningList/index',
  39. iconPath: '/static/images/tabbar/warning.png',
  40. selectedIconPath: '/static/images/tabbar/warning_select.png',
  41. // "text": "预警列表"
  42. },
  43. {
  44. pagePath: '/pages/mine/index',
  45. iconPath: '/static/images/tabbar/mine.png',
  46. selectedIconPath: '/static/images/tabbar/mine_select.png',
  47. // "text": "我的"
  48. },
  49. ];
  50. } else {
  51. //能源利用与分布系统
  52. _this.list = [
  53. {},
  54. {},
  55. {},
  56. {
  57. pagePath: '/pages/mine/index',
  58. iconPath: '/static/images/tabbar/mine.png',
  59. selectedIconPath: '/static/images/tabbar/mine_select.png',
  60. // "text": "我的"
  61. },
  62. ];
  63. }
  64. },
  65. methods: {
  66. switchTab(item, index) {
  67. this.currentIndex = index;
  68. let url = item.pagePath;
  69. uni.redirectTo({ url: url });
  70. },
  71. },
  72. };
  73. </script>
  74. <style lang="scss">
  75. .tab-bar {
  76. position: fixed;
  77. bottom: 0;
  78. left: 0;
  79. right: 0;
  80. height: 100rpx;
  81. background: #05112f;
  82. display: flex;
  83. justify-content: center;
  84. align-items: center;
  85. padding-bottom: env(safe-area-inset-bottom); // 适配iphoneX的底部
  86. .tab-bar-item {
  87. flex: 1;
  88. text-align: center;
  89. display: flex;
  90. justify-content: center;
  91. align-items: center;
  92. flex-direction: column;
  93. .tab_img {
  94. width: 37rpx;
  95. height: 41rpx;
  96. }
  97. .tab_text {
  98. font-size: 20rpx;
  99. margin-top: 9rpx;
  100. }
  101. }
  102. }
  103. </style>

这里需要注意:里面的图片路径要写成绝对路径,不然打包的时候如果是在该项目下的页面会出现层级问题,显示不出来图片

4、在main.js文件中将自定义的tabBar定义为全局组件。
  1. import tabBar from "components/tabBar/tabBar.vue"
  2. Vue.component('tabBar',tabBar)
5、在每一个原本将要设置为tabBar的子页面添加我们自定义的tabBar组件。
  1. //就在不同的tabbar页面中添加这一行 index 根据在tabbar中List集合中进行设置
  2. //第一个页面
  3. <tabBar selectedIndex = 0></tabBar>
  4. //第二个页面
  5. <tabBar selectedIndex = 1></tabBar>
6、创建一个新的页面来进行对不同系统进行操作

通过uni.setStorageSync('system', 'diagnosis'); 来判断进入的系统

  1. <template>
  2. <view>
  3. <uni-section title="请选择您要进入的系统" titleColor="#ffffff" type="line" class="titleStyle" />
  4. <view class="button-group">
  5. <button type="primary" plain="true" @click="handleButtonClick(1)">{{ energySystem }}</button>
  6. <button type="primary" plain="true" @click="handleButtonClick(2)">{{ diagnosisSystem }}</button>
  7. </view>
  8. </view>
  9. </template>
  10. <script>
  11. export default {
  12. data() {
  13. return {
  14. energySystem: '故障诊断系统',
  15. diagnosisSystem: '能源利用与分布系统',
  16. };
  17. },
  18. methods: {
  19. handleButtonClick(systemId) {
  20. if (systemId === 1) {
  21. // 进入故障诊断系统
  22. uni.setStorageSync('system', 'diagnosis');
  23. uni.navigateTo({
  24. url: '/pages/terbineList/index',
  25. });
  26. } else if (systemId === 2) {
  27. // 进入能源利用与分布系统
  28. uni.setStorageSync('system', 'energy');
  29. uni.navigateTo({
  30. url: '/pages/mine/index',
  31. });
  32. }
  33. },
  34. },
  35. };
  36. </script>
  37. <style>
  38. .titleStyle {
  39. font-size: 20rpx;
  40. }
  41. .button-group {
  42. flex-direction: column;
  43. align-items: center;
  44. width: 60%;
  45. height: auto;
  46. margin-left: 150rpx;
  47. }
  48. button {
  49. margin: 200rpx auto;
  50. }
  51. </style>
7.设置完后需要在每个tabbar页面中配置css  

一定要用这样的格式

  1. .tarbarStyle { //tarbarStyle
  2. position: fixed;
  3. bottom: 0;
  4. left: 0;
  5. right: 0;
  6. z-index: 99;
  7. }
  8. .dataInfo { //tabbar上面的信息展示
  9. margin-bottom: 50px; /* 根据 tabBar 的高度进行调整 */
  10. }

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

闽ICP备14008679号