matchRoutesdeclare function matchRoutes(
routes: RouteObject[],
location: Partial<Location> | string,
basename?: string
): RouteMatch[] | null;
interface RouteMatch<ParamKey extends string = string> {
params: Params<ParamKey>;
pathname: string;
route: RouteObject;
}
matchRoutes 对一组路由运行路由匹配算法,以查看哪些路由(如果有)与给定的 location 匹配。如果找到匹配项,则返回一个 RouteMatch 对象数组,每个匹配的路由对应一个对象。
这是 React Router 匹配算法的核心。它被 useRoutes 和 <Routes> 组件 内部使用,以确定哪些路由与当前位置匹配。在某些情况下,您可能希望手动匹配一组路由,它也可能很有用。