可以将路由进行嵌套使用

/router/index.ts:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { createRouter, createWebHistory } from "vue-router"
import type { RouteRecordRaw } from "vue-router"

const routes: Array<RouteRecordRaw> = [
{
path: "/",
component: () => import("../components/footer.vue"),
// 嵌套路由,可以无限嵌套
children: [
{
// path为空即默认展示该路由
path: "",
name: "Login",
component: () => import("../components/login.vue")
},
{
path: "reg",
name: "Reg",
component: () => import("../components/reg.vue")
}
]
},
]

const router = createRouter({
// 路由的模式
history: createWebHistory(),
// 路由信息
routes
})

export default router

footer.vue:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<template>
<div>
<!-- 这里将展示login或reg -->
<router-view></router-view>
<hr>
<h1>我是父路由</h1>
<div>
<router-link to="/">Login</router-link>
<router-link to="/reg" style="margin-left: 10px;">Reg</router-link>
<!-- 如果父路由不是根路由,比如是/user,则router-link的to必须带上/user -->
<!-- <router-link to="/user">Login</router-link> -->
<!-- <router-link to="/user/reg" style="margin-left: 10px;">Reg</router-link> -->
</div>
</div>
</template>

App.vue:

1
2
3
4
5
6
7
8
<template>
<div>
<h1>Vue-Router test</h1>
<hr>
<!-- 这里将展示footer -->
<router-view></router-view>
</div>
</template>