MyTabBar.vue 2.0 KB
<template>
  <div class="tab-bar-view">
    <div class="tab-bar-item" v-for="(item, index) in tabBarList" :key="index" :class="{ activity: activityTabBarIndex === index }" @click="itemClick(item, index)">
      <div v-if="item.icon" class="tab-bar-icon van-icon" :class="`van-icon-${item.icon}`" @click.stop="itemClick(item, index)"></div>
      <div class="tab-bar-name">{{ item.label }}</div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'MyTabBar',
  props: {
    value: {
      type: [String, Number],
      required: true
    },
    tabBarList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  computed: {
    activityTabBarIndex: {
      get() {
        let currentIndex = 0
        if (typeof this.value === 'string') {
          this.tabBarList.forEach((item, index) => {
            if (item.name === this.value) {
              currentIndex = index
            }
          })
        } else if (typeof this.value === 'number') {
          currentIndex = this.value
        }
        return currentIndex
      },
      set(index) {
        this.$emit('input', index)
      }
    }
  },
  data() {
    return {}
  },
  methods: {
    itemClick(item, index) {
      console.log('tabbar-itemClick-index', index)
      this.activityTabBarIndex = index
      this.$emit('change', item)
    }
  }
}
</script>

<style lang="scss" scoped>
.tab-bar-view {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 51px;
  display: flex;
  align-items: center;
  padding-bottom: constant(safe-area-inset-bottom);
  padding-bottom: env(safe-area-inset-bottom);
  background-color: #fff;
  box-shadow: 0 0 10px #d9d9d9;
  border-top: 1px solid #e3e3e3;
  .tab-bar-item {
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    flex: 1;
    font-size: 12px;
    line-height: 1;
    color: #7d7e80;
    cursor: pointer;
    .tab-bar-icon {
      margin-bottom: 4px;
      font-size: 22px;
    }
    &.activity {
      color: #1989fa;
    }
  }
}
</style>