Commit c85bf007 by chenwq1

feat: 添加底部导航栏图标及逻辑优化

- 新增底部导航栏图标文件,包括 mine、report 和 work order 的激活与非激活状态图标。
- 更新布局文件,使用新图标并优化 tabbar 的状态管理逻辑,确保导航栏在路由变化时正确更新。
1 parent 7b7e5776
Pipeline #92 for c85bf007 failed in 0 seconds
<script setup lang="ts"> <script setup lang="ts">
import { computed } from "vue"; import { computed, ref, watch } from "vue";
import { RouterView, useRoute, useRouter } from "vue-router"; import { RouterView, useRoute, useRouter } from "vue-router";
import tabMineOff from "@/assets/image/tab-mine-off.png";
import tabMineOn from "@/assets/image/tab-mine-on.png";
import tabReportOff from "@/assets/image/tab-report-off.png";
import tabReportOn from "@/assets/image/tab-report-on.png";
import tabWorkOrderOff from "@/assets/image/tab-work-order-off.png";
import tabWorkOrderOn from "@/assets/image/tab-work-order-on.png";
const route = useRoute(); const route = useRoute();
const router = useRouter(); const router = useRouter();
const showTabBar = computed(() => route.meta.showTabBar !== false); const showTabBar = computed(() => route.meta.showTabBar !== false);
const tabList = [ const tabList = [
{ routeName: "Home", title: "检测工单", icon: "todo-list-o", activeIcon: "todo-list" }, { routeName: "Home", title: "检测工单", icon: tabWorkOrderOff, activeIcon: tabWorkOrderOn },
{ routeName: "CarList", title: "检测报告", icon: "description", activeIcon: "description" }, { routeName: "CarList", title: "检测报告", icon: tabReportOff, activeIcon: tabReportOn },
{ routeName: "Mine", title: "我的", icon: "user-o", activeIcon: "user" }, { routeName: "Mine", title: "我的", icon: tabMineOff, activeIcon: tabMineOn },
]; ];
const activeTab = computed(() => { /**
const name = route.name as string | undefined; * 数字下标 + v-model;勿给 TabbarItem 绑字符串 name。
return tabList.findIndex((tab) => tab.routeName === name); * 变量勿命名 `active`:`#icon` 插槽会传入同名 prop,会遮蔽 setup 里的 ref,导致 tabbarIndex(false)→0、第一项误判为选中。
*/
const tabbarActive = ref(0);
/** 供模板(含 `#icon` 插槽)使用:在插槽内直接读 ref 可能解析失败→undefined→tabbarIndex 恒为 0,第一项图标永远「选中」 */
const currentTabbarIndex = computed(() => tabbarIndex(tabbarActive.value));
function tabbarIndex(val: unknown): number {
const n = typeof val === "number" ? val : Number(val);
return Number.isFinite(n) ? n : 0;
}
function routeToActiveIndex(): number {
const n = route.name;
if (typeof n === "string") {
const i = tabList.findIndex((t) => t.routeName === n);
if (i !== -1) return i;
}
const p = route.path;
if (p.includes("/home")) return 0;
if (p.includes("car-list")) return 1;
if (p.includes("/mine")) return 2;
return 0;
}
watch(
() => route.fullPath,
() => {
tabbarActive.value = routeToActiveIndex();
},
{ immediate: true },
);
/** 防止 v-model 偶发成字符串导致与 TabbarItem 内部 index 比较失败 */
watch(tabbarActive, (v: unknown) => {
const n = tabbarIndex(v);
if (n !== v) tabbarActive.value = n;
}); });
function onTabChange(index: number) { function onTabChange(index: number | string) {
const tab = tabList[index]; const i = tabbarIndex(index);
const tab = tabList[i];
if (tab) { if (tab) {
router.push({ name: tab.routeName }); router.push({ name: tab.routeName });
} }
...@@ -44,15 +88,19 @@ function onTabChange(index: number) { ...@@ -44,15 +88,19 @@ function onTabChange(index: number) {
</RouterView> </RouterView>
<van-tabbar v-if="showTabBar" <van-tabbar v-if="showTabBar"
active-color="var(--color-primary)" v-model="tabbarActive"
inactive-color="#7d7e80" active-color="#185efe"
:model-value="activeTab" inactive-color="#999999"
safe-area-inset-bottom safe-area-inset-bottom
@change="onTabChange"> @change="onTabChange">
<van-tabbar-item v-for="tab in tabList" <van-tabbar-item v-for="(tab, idx) in tabList"
:key="tab.routeName" :key="tab.routeName">
:icon="activeTab === tabList.indexOf(tab) ? tab.activeIcon : tab.icon">
{{ tab.title }} {{ tab.title }}
<template #icon>
<img alt=""
class="tabbar-item-icon"
:src="currentTabbarIndex === idx ? tab.activeIcon : tab.icon" />
</template>
</van-tabbar-item> </van-tabbar-item>
</van-tabbar> </van-tabbar>
</div> </div>
...@@ -72,6 +120,18 @@ function onTabChange(index: number) { ...@@ -72,6 +120,18 @@ function onTabChange(index: number) {
bottom: 0; bottom: 0;
width: 100%; width: 100%;
} }
:deep(.van-tabbar-item__text) {
font-size: 10px;
font-weight: 400;
}
}
.tabbar-item-icon {
display: block;
width: 22px;
height: 22px;
object-fit: contain;
} }
.fade-enter-active, .fade-enter-active,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!