Route 类表示 @esmx/router 中已解析的路由对象。每次导航都会产生一个 Route 实例,包含匹配路径、参数、查询字符串和关联元数据的完整信息。
enum RouteType {
push = 'push',
replace = 'replace',
restartApp = 'restartApp',
go = 'go',
forward = 'forward',
back = 'back',
unknown = 'unknown',
pushWindow = 'pushWindow',
replaceWindow = 'replaceWindow',
pushLayer = 'pushLayer'
}表示导航的触发方式:
push:标准前进导航(添加历史记录)replace:替换当前历史记录restartApp:应用重启导航go:通过 router.go() 的编程式历史遍历forward:通过 router.forward() 的前进导航back:通过 router.back() 的后退导航unknown:由浏览器 popstate 事件触发的导航pushWindow:在新窗口中打开导航replaceWindow:替换当前窗口位置pushLayer:在图层中打开导航interface RouteLocation {
path?: string;
url?: string | URL;
params?: Record<string, string>;
query?: Record<string, string | undefined>;
queryArray?: Record<string, string[] | undefined>;
hash?: string;
state?: RouteState;
keepScrollPosition?: boolean;
statusCode?: number | null;
layer?: RouteLayerOptions | null;
confirm?: RouteConfirmHook | null;
}用于编程式导航的路由位置对象:
path:路由路径url:完整 URL 字符串或 URL 对象params:动态片段的路由参数query:查询字符串参数(单值)queryArray:查询字符串参数(重复键的数组值)hash:URL 哈希片段state:持久化到历史记录中的自定义状态数据keepScrollPosition:为 true 时,导航后保持当前滚动位置statusCode:服务端响应的 HTTP 状态码layer:图层配置选项confirm:自定义确认处理器,覆盖默认的路由过渡逻辑type RouteLocationInput = RouteLocation | string;导航方法的输入类型。可以是简单的路径字符串或 RouteLocation 对象。
// 字符串简写
router.push('/user/123');
// 对象形式
router.push({
path: '/user',
query: { id: '123' },
hash: '#profile'
});RouteTypetrue创建此路由的导航类型。
stringtrue相对于路由器基础路径的路径。对于匹配的路由,这是移除基础前缀后的路径。对于未匹配的路由,等同于完整路径名。
// 基础 URL 为 'https://example.com/app/'
// 导航到 '/app/user/123'
route.path // '/user/123'stringtrue包含查询字符串和哈希的完整路径。
route.fullPath // '/user/123?tab=profile#bio'URLtrue此路由的完整 URL 对象。
route.url.href // 'https://example.com/app/user/123?tab=profile#bio'
route.url.pathname // '/app/user/123'
route.url.origin // 'https://example.com'Record<string, string>true从路径中提取的动态路由参数。对于多匹配参数,包含第一个值。
// 路由配置:{ path: '/user/:id' }
// URL:'/user/123'
route.params // { id: '123' }Record<string, string[]>true数组形式的动态路由参数,适用于可匹配多个片段的参数。
Record<string, string | undefined>true解析后的查询字符串参数。对于重复的键,包含第一个值。
// URL:'/search?q=vue&page=1'
route.query // { q: 'vue', page: '1' }Record<string, string[] | undefined>true数组形式的查询字符串参数,适用于重复的查询键。
// URL:'/filter?tag=vue&tag=react'
route.queryArray // { tag: ['vue', 'react'] }stringtrueURL 哈希片段(包含 # 前缀)。
// URL:'/page#section-2'
route.hash // '#section-2'RouteMetatrue来自匹配路由配置的路由元数据。如果没有匹配路由则返回空对象。
// 路由配置:{ path: '/admin', meta: { requiresAuth: true } }
route.meta // { requiresAuth: true }readonly RouteParsedConfig[]true从父到子匹配的路由配置数组。如果没有匹配路由则为空数组。
// 嵌套路由:/user/profile
route.matched // [userConfig, profileConfig]
route.matched.length // 2RouteParsedConfig | nulltrue最后(最深层)匹配的路由配置。如果没有匹配路由则为 null。
if (route.config) {
console.log('匹配的路由路径:', route.config.path);
}RouteStatetrue与此路由关联的自定义状态数据,持久化在浏览器历史记录中。
// 携带状态导航
router.push({ path: '/checkout', state: { cartId: 'abc' } });
// 访问状态
route.state // { cartId: 'abc' }booleantrue导航是否应保持当前滚动位置而不是滚动到顶部。
booleantrue此路由是否由推入式导航创建(push、pushWindow、pushLayer)。
number | nulltrue服务端渲染重定向的 HTTP 状态码。
IncomingMessage | nulltrueNode.js HTTP 请求对象(SSR 期间可用)。
ServerResponse | nulltrueNode.js HTTP 响应对象(SSR 期间可用)。
Record<string | symbol, any>true来自路由器选项的共享上下文对象。
Route创建当前路由实例的副本,保留相同的配置和状态。
const routeCopy = route.clone();