Vue / 系统课时
实现面包屑导航
实现面包屑导航
本课完成一个由当前匹配路由自动生成的面包屑组件:统一补充首页节点,支持字符串和函数式动态标题,并让目录层级或当前页面保持不可点击,避免把导航逻辑写死在页面模板中。
学习目标
- 使用
route.matched获取当前页面的完整路由层级 - 通过路由元信息配置静态标题、动态标题与点击规则
- 验证首页、详情页和不可点击节点的显示结果
约定路由元信息
面包屑需要稳定的数据来源。将标题和点击规则放进 meta,页面改名或路由调整时只修改路由配置。下面是片段示例,依赖项目已经创建 dashboard、user-list 和 user-detail 路由对应的页面组件。
ts
import type { RouteLocationNormalizedLoaded } from "vue-router"
{
path: "system/users",
name: "user-list",
component: () => import("@/views/system/UserList.vue"),
meta: {
title: "用户管理"
}
},
{
path: "system/users/:id",
name: "user-detail",
component: () => import("@/views/system/UserDetail.vue"),
meta: {
title: (route: RouteLocationNormalizedLoaded) =>
`用户 ${String(route.params.id)}`
}
}title 为字符串时直接显示;为函数时接收当前路由,可读取路径参数、查询参数或业务状态。目录节点可以设置 breadcrumbClickable: false,完全不参与面包屑则设置 breadcrumb: false。
从匹配路由生成节点
新建 src/components/AppBreadcrumb.vue。组件先固定加入首页,再遍历 route.matched。当前页始终不可点击;显式关闭点击的中间节点也只显示文本。
vue
<script setup lang="ts">
import { computed } from "vue"
import {
useRoute,
type RouteLocationNormalizedLoaded,
type RouteLocationRaw,
type RouteRecordNormalized
} from "vue-router"
type BreadcrumbTitle =
| string
| ((route: RouteLocationNormalizedLoaded) => string)
interface BreadcrumbItem {
label: string
to?: RouteLocationRaw
}
const route = useRoute()
function resolveTitle(record: RouteRecordNormalized): string {
const title = record.meta.title as BreadcrumbTitle | undefined
return typeof title === "function" ? title(route) : title ?? ""
}
function pickParams(path: string): Record<string, string | string[]> {
const names = [...path.matchAll(/:([A-Za-z0-9_]+)/g)].map(match => match[1])
return Object.fromEntries(
names
.filter(name => route.params[name] !== undefined)
.map(name => [name, route.params[name] as string | string[]])
)
}
const items = computed<BreadcrumbItem[]>(() => {
const matched = route.matched.filter(record => record.meta.breadcrumb !== false)
const result: BreadcrumbItem[] = [
{
label: "首页",
to: route.name === "dashboard" ? undefined : { name: "dashboard" }
}
]
for (const record of matched) {
if (record.name === "dashboard") continue
const label = resolveTitle(record)
if (!label) continue
const isCurrent = record === matched.at(-1)
const clickable = record.meta.breadcrumbClickable !== false
result.push({
label,
to: !isCurrent && clickable && record.name
? { name: record.name, params: pickParams(record.path) }
: undefined
})
}
return result
})
</script>
<template>
<nav aria-label="面包屑">
<ol class="breadcrumb">
<li v-for="(item, index) in items" :key="`${item.label}-${index}`">
<RouterLink v-if="item.to" :to="item.to">{{ item.label }}</RouterLink>
<span v-else aria-current="page">{{ item.label }}</span>
<span v-if="index < items.length - 1" class="separator">/</span>
</li>
</ol>
</nav>
</template>
<style scoped>
.breadcrumb {
display: flex;
gap: 8px;
margin: 0;
padding: 0;
list-style: none;
}
.separator {
margin-left: 8px;
color: #909399;
}
</style>预期结果
text
访问 /system/users/42 时显示:
首页 / 用户管理 / 用户 42
“首页”和“用户管理”可点击,“用户 42”仅显示文本。
访问 /dashboard 时只显示不可点击的“首页”。实际路由参数会随访问地址变化。pickParams 只传递目标路由真正声明的参数,避免把详情页的 id 错误带给不需要该参数的父级路由。
处理不可点击层级
布局路由或纯目录路由通常没有独立页面。此时保留标题可以表达层级,但应关闭跳转,防止用户点击后进入空壳页面。下面是依赖父子路由上下文的片段示例。
ts
{
path: "system",
meta: {
title: "系统管理",
breadcrumbClickable: false
},
children: [
// 具体业务路由
]
}验证时访问任一子页面,确认“系统管理”显示为普通文本,后续可访问的业务节点仍由 RouterLink 渲染。若某个布局节点不应显示,将其改为 breadcrumb: false。
按自己的节奏,确认真正理解。