You need to sign in or sign up before continuing.
tabbar.vue 1.65 KB
<template>
  <view class="tabbar">
    <up-tabbar
      :value="activePath"
      @change="handleTabChange"
      :placeholder="true"
      activeColor="#000"
      :fixed="true"
    >
      <up-tabbar-item
        v-for="(item, index) in tabList"
        :key="index"
        :text="item.text"
        :name="item.path"
      >
        <template #active-icon>
          <image :class="['icon', item.special ? 'mid-icon' : '']" :src="item.selectedIcon"></image>
        </template>
        <template #inactive-icon>
          <image :class="['icon', item.special ? 'mid-icon' : '']" :src="item.icon"></image>
        </template>
      </up-tabbar-item>
    </up-tabbar>
  </view>
</template>

<script setup>
  import { ref } from 'vue';
  import { onShow } from '@dcloudio/uni-app';

  const activePath = ref('pages/xunji/xunji');

  // 配置化 Tabbar
  const tabList = [
    {
      text: '训记',
      path: 'pages/xunji/xunji',
      icon: '/static/tabbar/xunji.png',
      selectedIcon: '/static/tabbar/xunji-sel.png',
      special: false,
    },
    {
      text: '我的',
      path: 'pages/user/user',
      icon: '/static/tabbar/wode.png',
      selectedIcon: '/static/tabbar/wode-sel.png',
      special: false,
    },
  ];

  const handleTabChange = (name) => {
    activePath.value = name;
    uni.switchTab({ url: '/' + name });
  };

  onShow(() => {
    const pages = getCurrentPages();
    const currPage = pages[pages.length - 1];
    if (currPage && !currPage.route.includes('entry')) {
      activePath.value = currPage.route;
    }
  });
</script>

<style lang="scss" scoped>
  .icon {
    width: 48rpx;
    height: 48rpx;
    transition: all 0.2s;
  }
</style>