import { baseRoutes } from "./router.config"; import { Route, RouteObject, Routes, useLocation } from "react-router-dom"; import { RouteGuard } from "./AuthGuard"; import { pathToRegexp } from "path-to-regexp"; import { defaultTitle } from "@/constant"; // export const RouterElements = () => useRoutes(routesConfig); export type ExpandRouteProps = { title?: string; }; export const RouterElements = () => { const location = useLocation(); /** * document.title */ const currentRoute = [...baseRoutes].find((r) => pathToRegexp(location.pathname).test(r.path!) ); document.title = currentRoute?.title ?? defaultTitle; /** * 递归children */ const generateRoutes = (routes: (RouteObject & ExpandRouteProps)[]) => routes.map((r) => ( {r.children && generateRoutes(r.children)} )); return ( {generateRoutes(baseRoutes)} 404} /> ); };