import { ROUTE_TITLE_MAP } from "./constants" import type { BreadcrumbItem } from "./types" /** 根据当前路径生成面包屑 */ export const generateBreadcrumb = (pathname: string): BreadcrumbItem[] => { const items: BreadcrumbItem[] = [{ label: "首页", path: "/app/dashboard" }] // 首页本身不需要面包屑 if (pathname === "/app" || pathname === "/app/dashboard") { return items } // 逐级拆分路径,生成中间层级 const segments = pathname.split("/").filter(Boolean) let currentPath = "" for (let i = 0; i < segments.length; i++) { currentPath += `/${segments[i]}` const title = ROUTE_TITLE_MAP[currentPath] if (title) { // 最后一级不带 path(当前页面,不可点击) const isLast = i === segments.length - 1 items.push({ label: title, path: isLast ? undefined : currentPath, }) } else { // 动态路由段(如 :id),用路径片段做 label const isLast = i === segments.length - 1 items.push({ label: segments[i], path: isLast ? undefined : currentPath, }) } } return items }