TabBar.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <template>
  2. <div class="tab-bar">
  3. <div
  4. class="tab-item"
  5. v-for="tab in tabs"
  6. :key="tab.path"
  7. @click="switchTab(tab.path)"
  8. :class="{ active: currentPath === tab.path }"
  9. >
  10. <img
  11. :src="getIcon(tab)"
  12. class="tab-icon"
  13. alt=""
  14. />
  15. <span>{{ tab.title }}</span>
  16. </div>
  17. </div>
  18. </template>
  19. <script>
  20. export default {
  21. name: 'TabBar',
  22. data() {
  23. return {
  24. currentPath: '/home',
  25. tabs: [
  26. {
  27. path: '/home',
  28. title: '首页',
  29. icon: 'index0.png',
  30. iconActive: 'index1.png'
  31. },
  32. {
  33. path: '/mall',
  34. title: '退休计划',
  35. icon: 'hang0.png',
  36. iconActive: 'hang1.png'
  37. },
  38. {
  39. // path: '/dynamic', //邀请百姓
  40. path: '/China', // 红旗医疗
  41. // path: '/Medical', // 红旗原始股
  42. title: '红旗基金',
  43. icon: 'gang0.png',
  44. iconActive: 'gang1.png'
  45. },
  46. {
  47. path: '/profile',
  48. title: '我的',
  49. icon: 'mine0.png',
  50. iconActive: 'mine1.png'
  51. }
  52. ]
  53. }
  54. },
  55. methods: {
  56. switchTab(path) {
  57. this.currentPath = path
  58. this.$router.push(path)
  59. },
  60. getIcon(tab) {
  61. return require(`@/assets/${this.currentPath === tab.path ? tab.iconActive : tab.icon}`)
  62. }
  63. },
  64. created() {
  65. this.currentPath = this.$route.path
  66. },
  67. watch: {
  68. '$route'(to) {
  69. this.currentPath = to.path
  70. }
  71. }
  72. }
  73. </script>
  74. <style lang="scss" scoped>
  75. .tab-bar {
  76. position: fixed;
  77. bottom: 0;
  78. left: 0;
  79. right: 0;
  80. height: 60px;
  81. background-color: #fff;
  82. display: flex;
  83. box-shadow: 0 -1px 5px rgba(0, 0, 0, 0.1);
  84. z-index: 999;
  85. .tab-item {
  86. flex: 1;
  87. display: flex;
  88. flex-direction: column;
  89. align-items: center;
  90. justify-content: center;
  91. color: #666;
  92. font-size: 14px;
  93. .tab-icon {
  94. width: 30px;
  95. height: 30px;
  96. margin-bottom: 2px;
  97. }
  98. &.active {
  99. color: #000;
  100. }
  101. }
  102. }
  103. </style>