Skip to content

RouteLocationNormalizedLoadedGeneric

当路由已经完全解析并加载完毕时的标准化路由结构。

Normalized(标准化):表示路由参数、路径、query 都经过解析与编码处理。

Loaded(已加载):所有匹配的异步组件(component: () => import(...))都已加载完毕。

对象阶段说明
RouteLocationRaw输入阶段你传给 router.push() 的对象
RouteLocationNormalizedGeneric解析阶段Vue Router 内部解析路径后得到的中间态
RouteLocationNormalizedLoadedGeneric已加载阶段当前激活的路由对象(useRoute() 的返回值)

Extends

RouteLocationNormalizedGeneric

Properties

属性名类型说明
fullPathstring完整路径,包含 query 和 hash,例如 /case/edit?id=1#form
hashstringURL 的 hash 部分(以 # 开头)
matchedRouteLocationMatched[]当前路由匹配到的所有路由记录(每个记录都包含已加载的组件)
metaRouteMeta合并后的 meta 信息,来自所有匹配路由记录
nameRouteRecordNameGeneric路由名称(如果定义了 name
paramsRouteParamsGeneric动态路由参数对象,例如 /user/:id{ id: '123' }
pathstring路径部分(不含 query 和 hash),例如 /case/edit
queryLocationQueryURL 查询参数,例如 ?a=1&b=2{ a: '1', b: '2' }
redirectedFromRouteLocationGeneric | undefined
ts
/case/edit/123?tab=info#top

那么 useRoute()(或 router.currentRoute.value)得到的对象大致是:

ts
{
  fullPath: "/case/edit/123?tab=info#top",
  hash: "#top",
  path: "/case/edit/123",
  name: "case-edit",
  params: { id: "123" },
  query: { tab: "info" },
  meta: { title: "编辑案件" },
  matched: [
    { path: "/case", components: { default: CaseLayout } },
    { path: "edit/:id", components: { default: CaseEdit } }
  ],
  redirectedFrom: undefined
}

fullPath

Inherited fromRouteLocationNormalizedGeneric.fullPath

redirectedFrom

Inherited from_RouteLocationBase.redirectedFrom

ts
 redirectedFrom : RouteLocationGeneric | undefined

当前路由若由重定向产生,则记录原始目标路由;否则为 undefined

ts
const route = useRoute()
const router = useRouter()

// 登录成功后
if (route.redirectedFrom) {
  router.push(route.redirectedFrom.fullPath)
} else {
  router.push('/')
}
ts
router.afterEach((to, from) => {
  if (to.redirectedFrom)
    console.log(`重定向自: ${to.redirectedFrom.fullPath}`)
})