RouteConfig
签名
ts
interface RouteConfig extends ExclusiveGuards {
path?: string;
name?: string;
state?: any;
sensitive?: boolean;
component?: ReactNode | ComponentLoader;
children?: RouteConfig[];
linkActiveClassName?: string;
linkInActiveClassName?: string;
linkExactActiveClassName?: string;
redirect?: string | RouterOptions | ((to: RouteConfig) => string | RouterOptions);
loader?: NonIndexRouteObject['loader'];
meta?: { [x: string]: any; loadingComponent?: ReactNode };
}参数
path: 路由路径,支持动态参数(如user/:id)。name: 命名路由标识,需全局唯一。component: 同步节点或异步组件 loader。children: 嵌套路由。redirect: 字符串、对象或函数形式重定向。beforeEnter: 来自ExclusiveGuards,可单函数或函数数组。meta: 任意元信息,会在useRoute().meta中合并体现。
返回值
RouteConfig 本身是声明类型,不是函数调用结果。
行为细节
beforeEnter在同一路由记录仅参数/query/hash变化时不会触发。- 当
component是异步 loader 时,可通过meta.loadingComponent指定加载态。 children中相对路径会按父级路径拼接。
错误/失败语义
- 重复
name会在运行时抛错。 redirect指向不存在的命名路由时会抛错。beforeEnter返回false/重定向对象时会触发导航失败或重定向流程。
示例
tsx
import type { RouteConfig } from '@vureact/router';
export const routes: RouteConfig[] = [
{
path: '/',
name: 'layout',
component: <Layout />,
meta: { layout: 'main' },
children: [
{ path: '', redirect: '/home' },
{ path: 'home', name: 'home', component: <HomePage /> },
{
path: 'user/:id',
name: 'user',
component: () => import('./UserPage'),
meta: { requiresAuth: true, loadingComponent: <div>Loading...</div> },
beforeEnter: (to, from, next) => {
if (!isAuthed()) return next('/login');
next();
},
},
],
},
];