vue3在routerjs中如何配置子路由
时间: 2024-05-11 21:14:02 浏览: 5
Vue3中的子路由可以通过在父级路由中使用`children`属性来配置。以下是一个示例:
```javascript
import { createRouter, createWebHistory } from 'vue-router'
const routes = [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/about',
name: 'About',
component: About,
children: [
{
path: '',
name: 'AboutDefault',
component: AboutDefault
},
{
path: 'team',
name: 'AboutTeam',
component: AboutTeam
},
{
path: 'contact',
name: 'AboutContact',
component: AboutContact
}
]
}
]
const router = createRouter({
history: createWebHistory(),
routes
})
export default router
```
在上面的示例中,父级路由`/about`定义了三个子路由`/about`, `/about/team`和`/about/contact`。其中,父级路由对应的组件是`About`,子路由对应的组件分别是`AboutDefault`、`AboutTeam`和`AboutContact`。
注意:子路由的`path`属性必须以父级路由的路径为前缀,否则无法匹配。例如,父级路由的路径为`/about`,子路由的路径可以是`/about/team`,但不能是`/team`。