嵌套路由需在父级路由配置中添加children数组,父路由必须有component,子路径不加/,父组件模板需含,空path可设默认页,children支持多层嵌套。

在父级组件对应的路由配置中添加 children 数组,就是配置嵌套路由的核心操作。它不是写在组件内部,而是写在 router/index.js(或类似路由配置文件)的路由对象里。
1. children 必须挂载在父路由对象下
只有当某个路由配置项包含 component(即有对应视图组件),才能在其下定义 children。子路由路径是相对父路径的,不加 / 开头:
- ✅ 正确写法:
{ path: '/user', component: User, children: [{ path: 'profile', component: Profile }] }→ 子路由完整路径为/user/profile - ❌ 错误写法:
{ path: '/user', children: [...] }→ 缺少component,该路由无法渲染,子路由也不会生效 - ❌ 错误写法:
{ path: '/user', children: [{ path: '/profile', ... }] }→/profile是绝对路径,会跳到根级,不嵌套
2. 父组件模板里必须有
children 配置只是“声明关系”,真正渲染子组件靠的是父组件模板中的 <router-view></router-view> —— 它是子路由内容的占位出口:
vue-composition-api-best-practices
Vue 3 组合式 API 与
热门下载
相关下载
精品课程
相关推荐
/
热门推荐
/
最新课程
最新文章
