二级路由
时间: 2023-07-27 09:19:11 浏览: 49
在Vue路由中,二级路由是指在某个路由下面再定义一组路由。例如,我们可以在`/about`路由下面定义`/about/info`和`/about/contact`两个子路由。
下面是二级路由的示例代码:
```javascript
const router = new VueRouter({
routes: [
{
path: '/',
component: Home
},
{
path: '/about',
component: About,
children: [
{
path: '',
component: Info
},
{
path: 'info',
component: Info
},
{
path: 'contact',
component: Contact
}
]
}
]
})
```
在上面的代码中,我们在`/about`路由下面定义了两个子路由:`/about/info`和`/about/contact`。使用`children`配置项指定子路由数组。
在`/about`路由的组件中可以使用`<router-view>`标签显示子路由对应的组件。
```html
<template>
<div>
<h2>About Page</h2>
<router-link to="/about/info">Info</router-link>
<router-link to="/about/contact">Contact</router-link>
<router-view></router-view>
</div>
</template>
```
在上面的代码中,我们使用`<router-view>`标签显示当前子路由对应的组件,同时使用`<router-link>`标签创建两个导航链接分别对应子路由`/about/info`和`/about/contact`。
使用二级路由可以让我们更加灵活地组织页面结构和路由层次,实现更加复杂的单页应用程序。