BottomNav.vue 1.3 KB
<template>
  <view class="fixed bottom-0 left-0 right-0 bg-white border-t border-gray-100 flex justify-around py-2">
    <view
      v-for="item in navItems"
      :key="item.path"
      @click="navigate(item.path)"
      :class="['flex flex-col items-center py-2 px-5', isActive(item.path) ? 'text-blue-600' : 'text-gray-500']"
    >
      <component :is="item.icon" size="24" />
      <span class="text-xs mt-1">{{ item.label }}</span>
      <view v-if="isActive(item.path)" class="absolute bottom-0 w-10 h-1 bg-blue-600 rounded-t-full"></view>
    </view>
  </view>
</template>

<script setup>
import { computed, shallowRef } from 'vue';
import Taro from '@tarojs/taro';
import { Home, Find, Shop, My } from '@nutui/icons-vue-taro';

const navItems = shallowRef([
  { path: '/pages/Dashboard/index', icon: Home, label: '首页' },
  { path: '/pages/Activities/index', icon: Find, label: '活动' },
  { path: '/pages/Rewards/index', icon: Shop, label: '兑换' },
  { path: '/pages/Profile/index', icon: My, label: '我的' },
]);

const currentPage = computed(() => {
  const pages = Taro.getCurrentPages();
  return pages.length > 0 ? '/' + pages[pages.length - 1].route : '';
});

const isActive = (path) => {
  return currentPage.value === path;
};

const navigate = (path) => {
  Taro.reLaunch({ url: path });
};
</script>