当前位置:   article > 正文

uniapp 动态tabBar(全过程包含vuex 组件 配置等)

uniapp 动态tabbar

   (装船权限)

 

 (卸船权限)

1.首先我们需要在pages.json配置tabbar  和pages (配置所有tabbar路径)

  1. "pages": [ //pages数组中第一项表示应用启动页,
  2. {
  3. "path": "pages/loadAndUnloadVessel/loadVessel/loadVesselPlan",
  4. "style": {
  5. "navigationBarTitleText": "装船作业计划",
  6. "app-plus": {
  7. "titleNView": false,
  8. "bounce": "none"
  9. }
  10. }
  11. }, {
  12. "path": "pages/loadAndUnloadVessel/loadVessel/loadVesselCommand",
  13. "style": {
  14. "navigationBarTitleText": "装船指令",
  15. "app-plus": {
  16. "titleNView": false,
  17. "bounce": "none"
  18. }
  19. }
  20. }, {
  21. "path": "pages/loadAndUnloadVessel/loadVessel/loadVesselHistory",
  22. "style": {
  23. "navigationBarTitleText": "历史",
  24. "app-plus": {
  25. "titleNView": false,
  26. "bounce": "none"
  27. }
  28. }
  29. }, {
  30. "path": "pages/loadAndUnloadVessel/unloadVessel/unloadVesselPlan",
  31. "style": {
  32. "navigationBarTitleText": "卸船作业计划",
  33. "app-plus": {
  34. "titleNView": false,
  35. "bounce": "none"
  36. }
  37. }
  38. }, {
  39. "path": "pages/loadAndUnloadVessel/unloadVessel/unloadVesselCommand",
  40. "style": {
  41. "navigationBarTitleText": "卸船指令",
  42. "app-plus": {
  43. "titleNView": false,
  44. "bounce": "none"
  45. }
  46. }
  47. }, {
  48. "path": "pages/loadAndUnloadVessel/unloadVessel/unloadVesselHistory",
  49. "style": {
  50. "navigationBarTitleText": "历史",
  51. "app-plus": {
  52. "titleNView": false,
  53. "bounce": "none"
  54. }
  55. }
  56. },
  57. ],
  58. "tabBar": {
  59. "color": "#7a7e83",
  60. "selectedColor": "#0faeff",
  61. "backgroundColor": "#ffffff",
  62. // "custom": true,custom(自定义),不开启的话,在页面是有占位的,就需要在页面进行隐藏
  63. "list": [{
  64. "pagePath": "pages/loadAndUnloadVessel/loadVessel/loadVesselPlan"
  65. }, {
  66. "pagePath": "pages/loadAndUnloadVessel/loadVessel/loadVesselCommand"
  67. },
  68. {
  69. "pagePath": "pages/loadAndUnloadVessel/loadVessel/loadVesselHistory"
  70. },
  71. {
  72. "pagePath": "pages/loadAndUnloadVessel/unloadVessel/unloadVesselPlan"
  73. }, {
  74. "pagePath": "pages/loadAndUnloadVessel/unloadVessel/unloadVesselCommand"
  75. },
  76. {
  77. "pagePath": "pages/loadAndUnloadVessel/unloadVessel/unloadVesselHistory"
  78. }
  79. ]
  80. },

2.配置动态tabBar.js

如图↓

 代码↓

  1. // 装船权限
  2. const loadVessel = [{
  3. "pagePath": "/pages/loadAndUnloadVessel/loadVessel/loadVesselPlan",
  4. "text": "装船作业计划",
  5. "iconPath": require("@/static/img/common/装船作业计划.png"),
  6. "selectedIconPath": require("@/static/img/common/装船作业计划1.png")
  7. }, {
  8. "pagePath": "/pages/loadAndUnloadVessel/loadVessel/loadVesselCommand",
  9. "text": "装船指令",
  10. "iconPath": require("@/static/img/common/装船指令.png"),
  11. "selectedIconPath": require("@/static/img/common/装船指令1.png")
  12. },
  13. {
  14. "pagePath": "/pages/loadAndUnloadVessel/loadVessel/loadVesselHistory",
  15. "text": "历史",
  16. "iconPath": require("@/static/img/common/历史.png"),
  17. "selectedIconPath": require("@/static/img/common/历史1.png")
  18. }
  19. ]
  20. //卸船权限
  21. const unloadVessel = [{
  22. "pagePath": "/pages/loadAndUnloadVessel/unloadVessel/unloadVesselPlan",
  23. "text": "卸船作业计划",
  24. "iconPath": require("@/static/img/common/装船作业计划.png"),
  25. "selectedIconPath": require("@/static/img/common/装船作业计划1.png")
  26. }, {
  27. "pagePath": "/pages/loadAndUnloadVessel/unloadVessel/unloadVesselCommand",
  28. "text": "卸船指令",
  29. "iconPath": require("@/static/img/common/装船指令.png"),
  30. "selectedIconPath": require("@/static/img/common/装船指令1.png")
  31. },
  32. {
  33. "pagePath": "/pages/loadAndUnloadVessel/unloadVessel/unloadVesselHistory",
  34. "text": "历史",
  35. "iconPath": require("@/static/img/common/历史.png"),
  36. "selectedIconPath": require("@/static/img/common/历史1.png")
  37. }
  38. ]
  39. export default {
  40. loadVessel,//装船权限名 最后要存入 isMemberType里
  41. unloadVessel//卸船权限名 最后要存入 isMemberType里
  42. }

3.使用vuex对tabBar列表数据进行一个存储赋值

index.js↓

  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import tabBar from './modules/tabBar'
  4. Vue.use(Vuex)
  5. const store = new Vuex.Store({
  6. modules:{tabBar},
  7. state: {
  8. },
  9. getters: {
  10. tabBarList: state => state.tabBar.tabBarList,
  11. isMemberType: state => state.tabBar.isMemberType,
  12. },
  13. mutations: {
  14. }
  15. })
  16. export default store

tabBar.js↓

  1. import tarBarUserType from '@/utils/tabBar.js';
  2. const tabBar = {
  3. state: {
  4. // 判断当前权限
  5. isMemberType: '',
  6. // tabbar列表数据
  7. tabBarList: []
  8. },
  9. mutations: {
  10. setType(state, isMemberType) {
  11. state.isMemberType = isMemberType;
  12. state.tabBarList = tarBarUserType[isMemberType];//根据权限取出对应的tabBar
  13. console.log(state.tabBarList )
  14. }
  15. }
  16. }
  17. export default tabBar;

创建一个tabBar组件↓

 代码↓

  1. <template>
  2. <view class="tab-bar">
  3. <view class="content">
  4. <view class="one-tab" v-for="(item, index) in tabBarList" :key="index" @click="selectTabBar(item.pagePath)">
  5. <view>
  6. <view class="tab-img">
  7. <image v-if="routePath === item.pagePath" class="img" :src="item.selectedIconPath"></image>
  8. <image v-else class="img" :src="item.iconPath"></image>
  9. </view>
  10. </view>
  11. <view class="tit">{{ item.text }}</view>
  12. </view>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. export default {
  18. props: {
  19. // 底部导航栏数据
  20. tabBarList: {
  21. type: Array,
  22. required: true
  23. },
  24. // 当前页面路径
  25. routePath: {
  26. type: String,
  27. required: true
  28. }
  29. },
  30. data() {
  31. return {};
  32. },
  33. methods: {
  34. selectTabBar(path) {
  35. // console.log(path)
  36. uni.switchTab({
  37. url: path
  38. })
  39. }
  40. },
  41. onLoad() {
  42. // console.log(this.tabBarList)
  43. }
  44. };
  45. </script>
  46. <style lang="scss">
  47. .tab-bar {
  48. position: fixed;
  49. bottom: 0;
  50. left: 0;
  51. width: 100vw;
  52. padding: 0rpx;
  53. // padding-bottom: calc(10rpx + constant(safe-area-inset-bottom));
  54. // padding-bottom: calc(10rpx + env(safe-area-inset-bottom));
  55. background-color: #fff;
  56. // background-color: red;
  57. // height:80rpx;
  58. .content {
  59. display: flex;
  60. flex-direction: row;
  61. .one-tab {
  62. display: flex;
  63. flex-direction: column;
  64. align-items: center;
  65. width: 100%;
  66. // background-color: pink;
  67. .tab-img {
  68. width: 50rpx;
  69. height: 50rpx;
  70. .img {
  71. width: 100%;
  72. height: 100%;
  73. }
  74. }
  75. .tit {
  76. font-size: 25rpx;
  77. transform: scale(0.7);
  78. }
  79. }
  80. }
  81. }
  82. </style>

5.在存在tabbar的页面中都需要引入组件,并传相关数据(routePath传入当前页面的tabbar路径)

  1. <template>
  2. <view class="content">
  3. 卸船指令
  4. <tabBarAction :tabBarList='tabBarList' routePath='/pages/loadAndUnloadVessel/unloadVessel/unloadVesselCommand'>
  5. </tabBarAction>
  6. </view>
  7. </template>
  8. <script>
  9. import {
  10. mapGetters,
  11. mapState
  12. } from 'vuex';
  13. import tabBarAction from '@/components/tabBarAction/tabBarAction'
  14. export default {
  15. data() {
  16. return {
  17. };
  18. },
  19. computed: {
  20. // 这里的tabBarList就是数据源,直接使用传值
  21. ...mapGetters(['tabBarList'])
  22. },
  23. components: {
  24. tabBarAction
  25. },
  26. }
  27. </script>
  28. <style lang="scss" scoped>
  29. .content {
  30. margin-top: 100rpx;
  31. height: 100%;
  32. }
  33. </style>

6.在需要的地方配置权限:↓

  1. this.$store.commit('setType', tabbar路径);
  2.                 uni.switchTab({
  3.                     url:tabbar路径
  4.                 })

7.在app.vue 里面隐藏tabBar

  1. onShow() {
  2. uni.hideTabBar({});
  3. },

本文参照,重新丰满过程↓

小程序自定义tabbar导航栏、动态控制tabbar功能实现(uniapp)_uniapp动态设置tabbar_别改我bug的博客-CSDN博客

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