Skip to content

RouteRecordRaw

它用来定义单个路由记录(Route Record),也就是我们在 routes 配置数组中写的每一项的类型。

ts
export interface RouteRecordRaw {
  path: string
  name?: RouteRecordName
  redirect?: RouteRecordRedirectOption
  alias?: string | string[]
  children?: RouteRecordRaw[]
  meta?: Record<string | number | symbol, unknown>
  component?: RawRouteComponent
  components?: Record<string, RawRouteComponent>
  props?: boolean | Record<string, any> | ((to: RouteLocationNormalized) => Record<string, any>)
  beforeEnter?: NavigationGuardWithThis<undefined> | NavigationGuardWithThis<undefined>[]
  caseSensitive?: boolean
  pathToRegexpOptions?: PathToRegexpOptions
}

Properties

字段类型说明
pathstring路由路径(必须)。支持动态参数 /user/:id
namestring命名路由(可选),方便使用 router.push({ name })
componentComponent对应要渲染的单个视图组件。
componentsRecord<string, Component>用于命名视图(多个 <router-view name="...">)。
redirect`stringobject
alias`stringstring[]`
childrenRouteRecordRaw[]子路由。
metaRecord<string, any>路由元信息,可自定义字段(如 title, icon 等)。
props`booleanobject
beforeEnterNavigationGuard路由独享守卫。
caseSensitiveboolean是否区分路径大小写。
pathToRegexpOptionsobject控制路径匹配的正则配置。
ts
const routes: RouteRecordRaw[] = [
  {
    path: '/user',
    component: () => import('@/layouts/UserLayout.vue'),
    children: [
      {
        path: 'list', // 实际路径为 /user/list
        name: 'UserList',
        component: () => import('@/views/user/List.vue'),
      },
      {
        path: ':id',
        name: 'UserDetail',
        component: () => import('@/views/user/Detail.vue'),
        props: true, // 自动把 route.params 作为 props 传入组件
      },
    ],
  },
]