Linux 服务器配置与管理(CentOS Stream 9)
以 CentOS Stream 9(RHEL 9 系)为实验环境的服务器配置与管理课程:从命令行基础、用户权限、磁盘与文件系统,到服务部署、网络与远程管理、安全加固与故障排查。每课时 45 分钟,讲义内附可整段复制粘贴执行的实验命令。
这里是孟凡的课程资料库。内容按「课程项目」分门别类,每一个项目都是一套完整的、 按 45 分钟一课时编排的讲义;每一课时都配了可以下载到本地的课堂练习资料包。 不管你是从头学,还是回头查某一个知识点,都能在这里直接找到。
以 CentOS Stream 9(RHEL 9 系)为实验环境的服务器配置与管理课程:从命令行基础、用户权限、磁盘与文件系统,到服务部署、网络与远程管理、安全加固与故障排查。每课时 45 分钟,讲义内附可整段复制粘贴执行的实验命令。
围绕 Spring、SpringMVC、MyBatis 与 Java Web 项目实践整理的课程讲义与课堂练习。
从 Vue 3 核心语法到 Element Plus 组件实操,按每课时 45 分钟编排的前端实训讲义。每节课配一份可直接粘贴进脚手架 src/App.vue 的完整代码,并提供课堂练习下载包。
以 CentOS Stream 9(RHEL 9 系)为实验环境的服务器配置与管理课程:从命令行基础、用户权限、磁盘与文件系统,到服务部署、网络与远程管理、安全加固与故障排查。每课时 45 分钟,讲义内附可整段复制粘贴执行的实验命令。
围绕 Spring、SpringMVC、MyBatis 与 Java Web 项目实践整理的课程讲义与课堂练习。
从 Vue 3 核心语法到 Element Plus 组件实操,按每课时 45 分钟编排的前端实训讲义。每节课配一份可直接粘贴进脚手架 src/App.vue 的完整代码,并提供课堂练习下载包。
第 29 课你学会了“一个页面用动态路由显示不同内容”。但真实网站几乎都是一层套一层的:
这种“框架不动、里面换内容”的结构,就是嵌套路由。 今天要解决三个问题:
/admin 时,里面默认显示什么(默认子路由 + redirect);<keep-alive>)。做一个迷你后台:/admin 是框架页,里面嵌“仪表盘 / 用户列表 / 用户详情 / 系统设置”四个子页;再加一个 /report 用命名视图同时填“主区”和“侧栏”。
| 做到什么 | 在哪里看 | 完成的证据 |
|---|---|---|
| 会做嵌套路由 | /admin 下的子页 |
框架不动,只有内容区随菜单切换。 |
| 理解默认子路由与 redirect | 直接访问 /admin |
自动跳到 /admin/dashboard。 |
| 会用 keep-alive 与命名视图 | 仪表盘计数器 / /report |
开关 keep-alive 计数是否保留;report 两区同时有内容。 |
第 29 课的动态路由与 <component :is>(今天的“子路由出口”就是再嵌一层 <component :is>)、第 8 课的动态组件、第 12 课的布局容器。嵌套路由本质就是“组件里再放一个动态组件出口”。
| 时间 | 老师带着做 | 学生留下什么 |
|---|---|---|
| 0—6 分钟 | 画出“后台框架 + 内容区”的草图,问“内容区怎么跟着菜单变”。 | 理解嵌套 = 父组件里再开一个出口。 |
| 6—20 分钟 | 讲 children 子路由、默认子路由、redirect。 |
会写带 children 的路由表。 |
| 20—30 分钟 | 讲父级 <router-view> 与子级 <router-view> 的关系。 |
理解“外层出口渲染框架、框架里再放内层出口”。 |
| 30—40 分钟 | 讲命名视图(components)与 <keep-alive>。 |
会一个路由填两块、会缓存子页。 |
| 40—45 分钟 | 粘贴本课 App.vue,把预期现象走一遍。 |
全部功能跑通。 |
复制第 29 课的工程,改名 lesson-vue-30。不需要新增依赖——本课继续用手写 hash 路由树演示嵌套。
第 31 课讲路由守卫与登录鉴权:不是谁都能进 /admin 的,没登录要先去登录页——这正是后台框架最常见的需求。
来源:本课自编 Vue 3 前端核心技能实训 · 语法依据 Vue 3.5 官方文档 · 组件依据 Element Plus 2.8 官方文档
| 名字 | 用熟悉的话理解 | 本课里长什么样 |
|---|---|---|
| 嵌套路由 | 路由里套路由:外层是框架,里层是内容。 | children: [{ path:'dashboard' }] |
| 父路由 / 子路由 | 框架规则 / 框架里的内容规则。 | /admin 是父,/admin/users 是子 |
| 默认子路由 | 进父路由时,里面默认显示哪个子页。 | redirect: '/admin/dashboard' |
redirect |
“访问 A 自动转到 B”。 | 访问 /admin → /admin/dashboard |
| 路由出口 | 页面“被填进来”的那个位置。 | 本课里父框架内的 <component :is> |
| 命名视图 | 一个路由同时填多个“具名区域”。 | components: { default:A, sidebar:B } |
<keep-alive> |
把组件“冻起来”,切走再切回不重新创建。 | 包住子路由出口 |
children 子路由到底怎么排真实 vue-router 里,子路由的 path 不写父路径前缀,框架会自动拼:
{
path: '/admin',
component: AdminLayout,
redirect: '/admin/dashboard', // 进 /admin 时默认显示 dashboard
children: [
{ path: 'dashboard', component: Dashboard }, // 实际地址 /admin/dashboard
{ path: 'users', component: UserList }, // 实际地址 /admin/users
{ path: 'users/:id', component: UserDetail }, // 嵌套 + 动态
{ path: 'settings', component: Settings }
]
}
这一步为什么这样写:父路由 component 是“框架”,它本身模板里要留一个出口给子路由。子路由只写相对路径(dashboard),框架会把它拼成 /admin/dashboard。记住:子路由的 path 不要带斜杠开头,那是相对父级的。
很多新手以为“嵌套路由是一个出口里套一个出口”。没错,但要分清谁渲染谁:
<router-view>(本课是 <component :is="topComponent">)渲染父组件(框架);<router-view>(本课是 AdminLayout 里的 <component :is="childComp">)渲染子组件(内容)。所以“外层框架不重挂载、只换里层内容”是天然成立的——因为框架组件实例一直都在,只是它内部的子出口换了孩子。这正是后台“菜单点了框架不动”的根本原因。
命名视图解决的是“一个路由想同时往好几个位置塞组件”。默认 <router-view> 是没名字的(叫 default),你还可以写 <router-view name="sidebar"/>。路由表里用 components(带 s)而不是 component:
{ path: '/report', components: { default: ReportMain, sidebar: ReportSide } }
keep-alive 解决的是“子页面切走再回来,状态别丢”。比如仪表盘里有个计数器,你切到用户列表再切回来,默认会重新创建仪表盘(计数归零);用 <keep-alive> 包住子出口,Vue 会把仪表盘“冻存”,回来直接解冻,计数还在。
<!-- 父框架里这样包住子出口 -->
<router-view v-slot="{ Component }">
<keep-alive>
<component :is="Component" />
</keep-alive>
</router-view>
path 写成 /dashboard(带斜杠),结果地址变成 //dashboard 或匹配不到。redirect 写成了字符串但指向一个不存在的子路由,死循环或空白。component(单数)而不是 components(复数),导致只有默认区有内容。本课
App.vue手写了路由树和匹配逻辑。正式项目请用官方写法,下面给你完整骨架。
src/router/index.js(嵌套 + 重定向 + 命名视图):
import { createRouter, createWebHashHistory } from 'vue-router'
import AdminLayout from '@/layout/AdminLayout.vue'
import Dashboard from '@/views/Dashboard.vue'
import UserList from '@/views/UserList.vue'
import UserDetail from '@/views/UserDetail.vue'
import Settings from '@/views/Settings.vue'
const routes = [
{ path: '/', component: () => import('@/views/Home.vue') },
{
path: '/admin',
component: AdminLayout,
redirect: '/admin/dashboard',
children: [
{ path: 'dashboard', component: Dashboard },
{ path: 'users', component: UserList },
{ path: 'users/:id', component: UserDetail, props: true },
{ path: 'settings', component: Settings }
]
},
{
path: '/report',
components: { default: () => import('@/views/ReportMain.vue'), sidebar: () => import('@/views/ReportSide.vue') }
}
]
export default createRouter({ history: createWebHashHistory(), routes })
AdminLayout.vue(父框架里放子出口 + keep-alive):
<template>
<el-container>
<el-aside width="200px"><el-menu router :default-active="$route.path">…</el-menu></el-aside>
<el-main>
<router-view v-slot="{ Component }">
<keep-alive>
<component :is="Component" />
</keep-alive>
</router-view>
</el-main>
</el-container>
</template>
对照一下:本课手写的 matchPath()(按 / 分段、逐层比对 children)正是 vue-router 解析嵌套路由时做的事;redirectTo 字段对应 redirect;AdminLayout 里的第二层 <component :is> 对应 <router-view v-slot>。
来源:本课自编 Vue 3 前端核心技能实训 · 语法依据 Vue 3.5 官方文档 · 组件依据 Element Plus 2.8 官方文档
<script setup>
import { ref, computed, h, KeepAlive, onMounted, onBeforeUnmount } from 'vue'
import {
ElConfigProvider, ElCard, ElButton, ElAlert, ElMenu, ElMenuItem,
ElSwitch, ElTag, ElDescriptions, ElDescriptionsItem, ElDivider
} from 'element-plus'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import 'element-plus/dist/index.css'
/* ==================== 假数据 ==================== */
const users = [
{ id: 1, name: '张三', role: '管理员' },
{ id: 2, name: '李四', role: '编辑' },
{ id: 3, name: '王五', role: '访客' }
]
/* ==================== 极简路由树(嵌套 + 重定向 + 命名视图,这几十行就是 vue-router 帮你做的事) ==================== */
const routeTree = [
{ path: '/', component: 'Home' },
{
path: '/admin',
component: 'AdminLayout',
redirect: '/admin/dashboard', // 访问 /admin 时重定向到默认子路由
children: [
{ path: 'dashboard', component: 'Dashboard' },
{ path: 'users', component: 'UserList' },
{ path: 'users/:id', component: 'UserDetail' },
{ path: 'settings', component: 'Settings' }
]
},
// 命名视图:一个路由同时填满“主区”和“侧边区”
{ path: '/report', component: 'NamedViewPage' }
]
function parseQuery(str) {
const query = {}
if (!str) return query
str.split('&').forEach(pair => {
if (!pair) return
const eq = pair.indexOf('=')
const k = decodeURIComponent(eq === -1 ? pair : pair.slice(0, eq))
const v = eq === -1 ? '' : decodeURIComponent(pair.slice(eq + 1))
query[k] = v
})
return query
}
// 匹配路径,返回 { matched:[外层, 内层?], params, redirectTo? }
function matchPath(path) {
const segs = path.split('/').filter(Boolean)
for (const top of routeTree) {
const topSegs = top.path === '/' ? [] : top.path.split('/').filter(Boolean)
if (topSegs.length === 0) {
if (segs.length === 0) {
if (top.redirect) return { matched: [top.component], params: {}, redirectTo: top.redirect }
return { matched: [top.component], params: {} }
}
continue
}
if (segs.length < topSegs.length) continue
const params = {}
let ok = true
for (let i = 0; i < topSegs.length; i++) {
if (topSegs[i].startsWith(':')) params[topSegs[i].slice(1)] = decodeURIComponent(segs[i])
else if (topSegs[i] !== segs[i]) { ok = false; break }
}
if (!ok) continue
const rest = segs.slice(topSegs.length)
if (!top.children || top.children.length === 0) {
if (rest.length === 0) return { matched: [top.component], params }
continue
}
// 有子路由
if (rest.length === 0) {
if (top.redirect) return { matched: [top.component], params: {}, redirectTo: top.redirect }
return { matched: [top.component, 'EmptyChild'], params }
}
for (const child of top.children) {
const childSegs = child.path.split('/').filter(Boolean)
if (childSegs.length !== rest.length) continue
const cparams = {}
let cok = true
for (let i = 0; i < childSegs.length; i++) {
if (childSegs[i].startsWith(':')) cparams[childSegs[i].slice(1)] = decodeURIComponent(rest[i])
else if (childSegs[i] !== rest[i]) { cok = false; break }
}
if (cok) return { matched: [top.component, child.component], params: { ...params, ...cparams } }
}
return { matched: [top.component, 'NotFound'], params }
}
return { matched: ['NotFound'], params: {} }
}
function parseHash() {
const raw = location.hash.slice(1) || '/'
const qIndex = raw.indexOf('?')
const path = qIndex === -1 ? raw : raw.slice(0, qIndex)
const query = parseQuery(qIndex === -1 ? '' : raw.slice(qIndex + 1))
const m = matchPath(path)
return { path, query, ...m }
}
const currentRoute = ref(parseHash())
const currentHash = ref(location.hash || '(空)')
function applyRoute() {
const r = parseHash()
if (r.redirectTo) {
location.hash = '#' + r.redirectTo // 触发一次新的 hashchange,完成重定向
return
}
currentRoute.value = r
currentHash.value = location.hash || '(空)'
const titleMap = { Home: '首页', AdminLayout: '后台', NamedViewPage: '报表', NotFound: '不存在' }
document.title = (titleMap[currentRoute.value.matched[0]] || '演示') + ' · 嵌套路由演示'
}
onMounted(() => window.addEventListener('hashchange', applyRoute))
onBeforeUnmount(() => window.removeEventListener('hashchange', applyRoute))
/* ==================== 页面组件(用 h 写,保证即贴即跑,不依赖运行时编译器) ==================== */
const HomePage = {
setup() {
return () => h(ElCard, { class: 'block' }, {
header: () => '首页(/)',
default: () => [
h('p', { class: 'muted' }, '本课演示三件事:① 嵌套路由(后台“框架+内容”);② 默认子路由与 redirect;③ 命名视图 + keep-alive。'),
h('div', { class: 'btn-row' }, [
h(ElButton, { type: 'primary', onClick: () => location.hash = '#/admin' }, '进入后台(/admin,会自动重定向到 dashboard)'),
h(ElButton, { onClick: () => location.hash = '#/report' }, '进入命名视图报表(/report)')
])
]
})
}
}
// 仪表盘:组件内部有状态,用来演示 keep-alive 是否把它缓存住
const Dashboard = {
setup() {
const count = ref(0)
const mountedAt = ref(new Date().toLocaleTimeString())
return () => h(ElCard, { class: 'block' }, {
header: () => '仪表盘(/admin/dashboard)',
default: () => [
h('p', null, '这是一个子页面。它内部有一个计数器和“挂载时间”。'),
h(ElButton, { onClick: () => count.value++ }, '点我计数(当前 ' + count.value + ')'),
h('p', { class: 'muted', style: 'margin-top:8px' }, '本组件挂载于:' + mountedAt.value),
h('p', { class: 'muted' }, '开着 keep-alive 去别的子页再回来,计数和时间都还在;关掉 keep-alive 再回来,会重新挂载、计数归零。')
]
})
}
}
const UserList = {
setup() {
return () => h(ElCard, { class: 'block' }, {
header: () => '用户列表(/admin/users)',
default: () => [
h('div', { class: 'btn-row' },
users.map(u => h(ElButton, { key: u.id, plain: true, onClick: () => location.hash = '#/admin/users/' + u.id }, () => '查看 ' + u.name))
),
h('p', { class: 'muted', style: 'margin-top:8px' }, '点按钮进入“嵌套 + 动态”子路由 /admin/users/:id。')
]
})
}
}
const UserDetail = {
setup() {
const id = computed(() => currentRoute.value.params.id)
const user = computed(() => users.find(u => String(u.id) === id.value) || null)
return () => {
if (!user.value) return h(ElAlert, { type: 'error', title: '找不到用户 ' + id.value, closable: false, showIcon: true })
return h(ElCard, { class: 'block' }, {
header: () => '用户详情(/admin/users/' + id.value + ')',
default: () => h(ElDescriptions, { column: 1, border: true, size: 'small' }, {
default: () => [
h(ElDescriptionsItem, { label: 'ID(来自嵌套动态路由 :id)' }, () => String(user.value.id)),
h(ElDescriptionsItem, { label: '姓名' }, () => user.value.name),
h(ElDescriptionsItem, { label: '角色' }, () => user.value.role)
]
})
})
}
}
}
const Settings = {
setup() {
return () => h(ElCard, { class: 'block' }, {
header: () => '系统设置(/admin/settings)',
default: () => h('p', { class: 'muted' }, '这是另一个子页面,用来验证“切子路由时外层 AdminLayout 不重挂载,只有内层出口在变”。')
})
}
}
// 后台框架:外层组件,内部用 <component :is> 渲染“子路由出口”
const childComponents = { Dashboard, UserList, UserDetail, Settings, EmptyChild: null, NotFound: null }
const AdminLayout = {
setup() {
const childName = computed(() => currentRoute.value.matched[1])
const useKeepAlive = ref(true)
const childComp = computed(() => childComponents[childName.value])
return () => h('div', { class: 'admin' }, [
h(ElMenu, {
mode: 'vertical',
defaultActive: '#' + currentRoute.value.path,
class: 'admin-menu',
onSelect: (idx) => { location.hash = idx }
}, {
default: () => [
h(ElMenuItem, { index: '#/admin/dashboard' }, () => '仪表盘'),
h(ElMenuItem, { index: '#/admin/users' }, () => '用户列表'),
h(ElMenuItem, { index: '#/admin/settings' }, () => '系统设置')
]
}),
h('div', { class: 'admin-body' }, [
h('div', { class: 'admin-bar' }, [
h(ElButton, { size: 'small', onClick: () => location.hash = '#/' }, '← 退出后台'),
h(ElSwitch, {
modelValue: useKeepAlive.value,
'onUpdate:modelValue': (v) => { useKeepAlive.value = v },
activeText: '开启 keep-alive(缓存子页)',
inactiveText: '关闭 keep-alive'
})
]),
// 父级 <component :is> 在这里渲染子级出口
useKeepAlive.value
? h(KeepAlive, null, () => h(childComp.value || NotFound))
: h(childComp.value || NotFound)
])
])
}
}
// 命名视图:一个路由同时填满“主区”和“侧边区”
const ReportMain = {
setup() {
return () => h(ElCard, { class: 'block' }, {
header: () => '报表主区(默认 router-view)',
default: () => h('p', { class: 'muted' }, '这里是命名视图的“默认区”内容,对应 <router-view/>。')
})
}
}
const ReportSide = {
setup() {
return () => h(ElCard, { class: 'block' }, {
header: () => '报表侧栏(router-view name="sidebar")',
default: () => h('p', { class: 'muted' }, '这里是“具名区”内容,对应 <router-view name="sidebar"/>。同一路由一次匹配,两块区域一起填。')
})
}
}
const NamedViewPage = {
setup() {
return () => h('div', { class: 'named' }, [
h('p', { class: 'muted' }, '命名视图:一个路由可以把不同组件填到“默认区”和“具名区”。真实 vue-router 写法是 components: { default: A, sidebar: B },模板里用 <router-view/> 和 <router-view name="sidebar"/>。'),
h('div', { class: 'named-row' }, [
h('div', { class: 'named-main' }, [ h(ReportMain) ]),
h('div', { class: 'named-side' }, [ h(ReportSide) ])
])
])
}
}
const NotFound = {
setup() {
return () => h(ElAlert, { type: 'warning', title: '页面不存在(路径没有匹配到任何路由)', closable: false, showIcon: true })
}
}
const topComponent = computed(() => {
const name = currentRoute.value.matched[0]
return { Home: HomePage, AdminLayout, NamedViewPage, NotFound }[name] || NotFound
})
const pageProps = computed(() => ({ ...currentRoute.value.params }))
const routeInfo = computed(() => ({
hash: currentHash.value,
path: currentRoute.value.path,
matched: JSON.stringify(currentRoute.value.matched),
params: JSON.stringify(currentRoute.value.params),
query: JSON.stringify(currentRoute.value.query)
}))
</script>
<template>
<el-config-provider :locale="zhCn">
<div class="page">
<h1 class="title">嵌套路由与命名视图</h1>
<el-alert type="info" :closable="false" show-icon style="margin-bottom:16px">
<template #default>
<div>当前地址栏:<code>{{ routeInfo.hash }}</code></div>
<div>匹配到的组件链:<b>{{ routeInfo.matched }}</b></div>
<div>params:<b>{{ routeInfo.params }}</b> ,query:<b>{{ routeInfo.query }}</b></div>
</template>
</el-alert>
<!-- 顶层 <component :is>:根据 matched[0] 渲染 Home / AdminLayout / NamedViewPage -->
<component :is="topComponent" v-bind="pageProps" />
<el-alert
title="要点:① 嵌套路由 = 父组件里再放一个 <component :is> 当子出口;② 默认子路由用 redirect 顶上;③ 命名视图一个路由填多块;④ keep-alive 缓存子页面不重挂载。"
type="success" :closable="false" show-icon style="margin-top:16px"
/>
</div>
</el-config-provider>
</template>
<style>
html, body, #app {
margin: 0; padding: 0; max-width: none; width: auto; height: auto;
display: block; place-items: initial; text-align: left; background: #f5f7fa;
}
</style>
<style scoped>
.page { max-width: 1000px; margin: 28px auto; padding: 0 16px; }
.title { font-size: 22px; color: #303133; margin: 0 0 18px; }
.block { margin-bottom: 16px; }
.muted { color: #909399; font-size: 13px; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; margin: 6px 0; }
code { background: #1f2430; color: #a9dc76; padding: 2px 6px; border-radius: 4px; font-size: 13px; }
.admin { display: flex; gap: 16px; align-items: flex-start; }
.admin-menu { width: 180px; min-width: 180px; }
.admin-body { flex: 1; min-width: 0; }
.admin-bar { display: flex; gap: 16px; align-items: center; margin-bottom: 12px; flex-wrap: wrap; }
.named-row { display: flex; gap: 16px; }
.named-main { flex: 1; min-width: 0; }
.named-side { width: 260px; min-width: 260px; }
</style>
#/admin,立刻又被改成 #/admin/dashboard(重定向生效),右侧显示仪表盘。/admin/users/1,显示张三详情,ID 来自嵌套动态路由 :id。["AdminLayout","Dashboard"] 这种两层结构。#/xxx,显示“页面不存在”。访问 /admin 时,为什么地址栏会“闪一下”变成 /admin/dashboard?
因为本课在 applyRoute 里检测到 redirectTo 后,主动把 location.hash 改成了目标地址,从而再触发一次 hashchange 去匹配真正的子路由。 真实 vue-router 的 redirect 也是同样的思路:访问 A 时内部把要去的地址换成 B 再继续匹配。这一“闪”的本质是“先匹配父、再补一个默认子”,在 hash 路由里表现为地址栏变化;用 history 模式(无 #)时用户甚至察觉不到。
来源:本课自编 Vue 3 前端核心技能实训 · 语法依据 Vue 3.5 官方文档 · 组件依据 Element Plus 2.8 官方文档
const routeTree = [
{ path: '/', component: 'Home' },
{ path: '/admin', component: 'AdminLayout' }
]
预期:访问 #/admin 能看到 AdminLayout 的外壳(哪怕里面还没内容)。这一步确认“顶层 <component :is> 能渲染父组件”。
/admin 加 children 和默认子路由{
path: '/admin', component: 'AdminLayout',
redirect: '/admin/dashboard',
children: [ { path: 'dashboard', component: 'Dashboard' } ]
}
预期:访问 #/admin 自动变成 #/admin/dashboard,框架里出现仪表盘。
// AdminLayout 的渲染里:
h(KeepAlive, null, () => h(childComp.value))
预期:菜单点不同子页,只有这一块在变,框架其余部分不动。
{ path: 'users/:id', component: 'UserDetail' }
预期:访问 #/admin/users/2,UserDetail 能从 params.id 拿到 2。
在 Dashboard 里放一个 ref(0) 的计数器,开着/关着 keep-alive 各切一次再回来。预期:开着计数保留,关着重置。这一步让你“看见”缓存。
{ path: '/report', component: 'NamedViewPage' }
// NamedViewPage 内部同时渲染 ReportMain 与 ReportSide
预期:进入 /report,两个区域都有内容,证明“一个路由填多块”。
把第 3 页的完整 App.vue 覆盖 src/App.vue,把七条“预期看见什么”全走一遍。
| 位置 | 本次作用 |
|---|---|
routeTree |
路由树,含 children 与 redirect。 |
matchPath |
按段匹配父/子,支持 :id 与 redirectTo。 |
AdminLayout |
外层框架,内部第二个 <component :is> 是子出口。 |
childComponents / childComp |
子组件映射与当前子组件。 |
KeepAlive |
包住子出口,缓存子页面。 |
NamedViewPage |
命名视图演示(一路由填两块)。 |
| 操作或输入 | 预期观察 |
|---|---|
访问 #/admin |
自动到 #/admin/dashboard。 |
| 点菜单切子页 | 框架不动,仅内容区变。 |
| 开/关 keep-alive 后切换回来 | 开的计数保留,关的归零。 |
访问 #/admin/users/2 |
显示 2 号用户详情。 |
访问 #/report |
主区与侧栏同时有内容。 |
访问 #/xxx |
显示“页面不存在”。 |
来源:本课自编 Vue 3 前端核心技能实训 · 语法依据 Vue 3.5 官方文档 · 组件依据 Element Plus 2.8 官方文档
/admin/users/:id/log(用户操作日志),体会“三层路由”。matched 链显示“后台 / 用户列表 / 详情”,为第 32 课铺垫。| 步骤 | 改哪里 | 运行后应该看到什么 |
|---|---|---|
| 1 换业务 | routeTree 的 children + 子组件 |
自己的后台子页。 |
| 2 验缓存 | 子页加 ref 状态 + 开关 keep-alive |
开关影响状态是否保留。 |
| 3 加层级 | 再加一层 children |
三层地址能匹配。 |
| 4 命名视图 | 新增 NamedViewPage 式结构 |
一路由填两块。 |
| 5 面包屑 | 根据 matched 拼文字 |
显示层级路径。 |
| 检查项 | 通过条件 |
|---|---|
| 嵌套路由生效 | 父框架不动,子内容随菜单切换。 |
| 默认子路由 / redirect | 访问父路径能自动落到默认子页。 |
| keep-alive 生效 | 能演示“开着保留、关着重置”。 |
redirect 在哪。children 里的 path 为什么不用带父路径前缀。提交 src/App.vue 与 练习记录.md。
来源:本课自编 Vue 3 前端核心技能实训 · 语法依据 Vue 3.5 官方文档 · 组件依据 Element Plus 2.8 官方文档
| 看到什么现象 / 报错 | 先检查哪里 | 修好后怎样确认 |
|---|---|---|
访问 /admin 空白或报循环 |
redirect 指向了一个不存在/会再次触发 redirect 的地址 |
改成真实存在的子路径后正常。 |
| 子页面出不来 | AdminLayout 里忘了放“子出口” <component :is> |
加上后内容出现。 |
| 子路由匹配不到 | children 的 path 带了 / 前缀或被拼错 |
改成相对路径后匹配。 |
| 框架跟着子页一起重挂载 | 误把“子出口”放到了顶层(两层出口并列) | 改成“父里套子”后框架稳定。 |
| keep-alive 没生效 | 没用 <keep-alive> 包住子出口,或子组件每次都新建 |
包住后状态保留。 |
| 命名视图只有一块有内容 | 路由表用了 component(单数)而非 components |
改成 components 后两块都有。 |
:id 在嵌套里取不到 |
在父组件读 params.id,其实在子组件才有 |
到子组件读或从 matched 链取。 |
| 菜单高亮错乱 | default-active 与菜单 index 没对上 |
对齐后高亮正确(第 32 课细讲)。 |
/admin 空白,是 redirect 指错地方; 子页出不来,是父里没放子出口; 状态切走就丢,是没包 keep-alive; 命名视图缺一块,是 component 少写了 s。
v-if 手动判断“当前该显示哪个子页”来代替嵌套路由。路由的职责就是这件事,手写 v-if 既难维护又丢了对 URL 的响应。keep-alive 包在顶层出口外还指望缓存子页。它必须紧贴“子出口”才能真正冻存子组件实例。你的 routeTree(含 children 与 redirect)、你访问的完整 URL、你期望的 matched 链、实际显示的画面、AdminLayout 里子出口的写法。
/admin 为什么会变成 /admin/dashboard?(能答“redirect 默认子路由”)来源:本课自编 Vue 3 前端核心技能实训 · 语法依据 Vue 3.5 官方文档 · 组件依据 Element Plus 2.8 官方文档
访问 /admin 时地址栏为什么“闪一下”变成 /admin/dashboard?
参考解释:本课检测到路由带 redirect,于是主动改写 location.hash 触发再次匹配;真实 vue-router 的 redirect 同理。本质是“先落父路由,再补一个默认子路由”,在 hash 模式下表现为地址变化。
| 问题 | 参考解释 |
|---|---|
| 父级出口和子级出口是什么关系? | 顶层出口渲染“框架”,框架组件内部再放一个出口渲染“子内容”;框架实例一直活着,只有子出口在换。 |
| 命名视图和单个出口有什么区别? | 单个出口一次只填一个组件;命名视图(components)一次匹配,把不同组件填到默认区和具名区。 |
| 考考自己 | 可以这样通俗回答 |
|---|---|
children 里的 path 怎么写? |
写相对父级的,不带 / 前缀,如 dashboard。 |
| 默认子路由怎么实现? | 在父路由写 redirect 指向某个子路径。 |
| 为什么框架不随子页重挂载? | 框架是父组件实例,一直存在,只是内部子出口换了孩子。 |
| keep-alive 解决什么? | 切走子页再回来时,保留它的状态、不重新创建。 |
| 命名视图路由表用什么字段? | components(复数),按名字对应多个区域。 |
/admin,点菜单,强调“左边框架不动、右边换内容”。#/admin 回车,指出自动变 #/admin/dashboard。src/App.vue 与 练习记录.md 已保存。把本课手写的 matchPath(按 / 分段逐层比对 children)和正式 vue-router 的嵌套匹配对着读:写下一句话,你觉得手写嵌套路由最容易在哪一步出错?为什么?
来源:本课自编 Vue 3 前端核心技能实训 · 语法依据 Vue 3.5 官方文档 · 组件依据 Element Plus 2.8 官方文档