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 的完整代码,并提供课堂练习下载包。
你已经会做单页里的各种功能了:列表、表单、弹窗、树形控件……但有一个问题一直没解决——一个网站里有很多“页面”,比如“学生列表页”和“学生详情页”,它们是怎么切换的? 到现在为止,我们所有东西都堆在一个 App.vue 里,靠 v-if 切来切去。那不是真正的“页面切换”。
今天开始,我们进入前端路由这一大块。路由要解决的核心问题是:
/student/1 和 /student/2 是不同学生)。你要知道:后面 5 课(29—33)讲的都是“单页应用(SPA)怎么组织多个页面 + 怎么控制访问权限 + 怎么和后端要数据”。这是从“会写组件”到“能写完整网站”的关键一跃。
做一个“学生管理系统”的小前台:
/)展示所有学生;/student/3),URL 里带了学生编号 3;?tab=score 这种方式传。| 做到什么 | 在哪里看 | 完成的证据 |
|---|---|---|
会用动态路由 :id |
详情页 URL | 访问 #/student/3 能显示 3 号学生的资料。 |
| 分得清 query 与 params | 本课 App.vue 顶部解析面板 |
能说清哪个参数在 URL 里、哪个刷新会丢。 |
| 会用 props 接收参数 | 详情页组件 | 详情页用 props 拿到 id,而不是自己读 URL。 |
第 7 课的组件 props 通信(今天参数就是“路由传给页面组件的 props”)、第 8 课的动态组件 <component :is>(今天的“按路由切页面”本质上就是动态组件)、第 11 课的 Element Plus 引入方式。如果你忘了 props 怎么写,今天会重新用一次,正好复习。
| 时间 | 老师带着做 | 学生留下什么 |
|---|---|---|
| 0—6 分钟 | 问“点列表里的张三,详情页怎么知道是张三?”引出“页面之间要传参数”。 | 理解“页面切换 + 传参”是路由要解决的问题。 |
| 6—20 分钟 | 讲 query 与 params 的区别、动态路由 :id、对象写法与路径写法。 |
会写 #/student/3?tab=score 这种地址。 |
| 20—30 分钟 | 讲 props: true 让组件用 props 收参数,并演示“刷新后 params 消失”。 |
理解“路径参数”与“内存参数”的差异。 |
| 30—40 分钟 | 讲正规 vue-router 写法(安装、main.js、router/index.js)。 | 知道正式项目长什么样。 |
| 40—45 分钟 | 粘贴本课 App.vue,把三条“预期看见什么”走一遍。 |
全部功能跑通。 |
复制上一课的工程,改名 lesson-vue-29。不需要新增依赖——本课为了“即贴即跑”,用手写 hash 路由,没用真正的 vue-router。
第 30 课讲嵌套路由与命名视图:后台那种“左边菜单 + 右边内容、内容里还能再套子页面”的结构,就是靠它做的。
来源:本课自编 Vue 3 前端核心技能实训 · 语法依据 Vue 3.5 官方文档 · 组件依据 Element Plus 2.8 官方文档
| 名字 | 用熟悉的话理解 | 本课里长什么样 |
|---|---|---|
| 路由(route) | 一条“URL 规则 → 显示哪个页面”的对应关系。 | { path: '/student/:id', component: 'StudentDetail' } |
| 路由表 | 所有路由规则排成的清单。 | 本课的 routes 数组 |
| 查询参数(query) | 写在 ? 后面的键值对,像网址里的“备注”。 |
#/student/3?tab=score 里的 tab=score |
| 路径参数(params) | 藏在路径里的变量,让同一个页面显示不同内容。 | /student/:id 里的 :id,实际是 3 |
| 动态路由 | 带 :变量 的路由,一匹配多条真实 URL。 |
/student/:id 同时匹配 /student/1、/student/2…… |
router.push |
“跳转”的意思,点按钮去另一个页面。 | 本课用 location.hash = '...' 模拟 |
props: true |
让路由的“路径参数”直接变成组件的 props。 |
详情页 props: ['id'] 拿到 3 |
| hash 路由 | 用网址里 # 后面的部分当路由(不用后端配合)。 |
#/student/3 |
最本质的区别一句话:query 写在 URL 里、大家都看得见;params 里的“路径参数”也在 URL 里,但“内存参数”只在代码里、一刷新就没。
#/student/3?tab=score
└─ 3 是路径参数(params 里的 id)
└─ tab=score 是查询参数(query)
? 后面,刷新不丢,适合“非必须的、可分享的”信息(比如当前看的是“成绩”还是“资料”)。/student/:id 里的 id 直接拼在路径中,也在 URL 里,刷新也不丢。这才是动态路由能“根据 URL 显示不同学生”的根本原因。router.push({ name:'StudentDetail', params:{ id:3 } }) 但路由没写 :id)会把参数只存在内存里,刷新页面就没了。这是新手最常踩的坑。:id 与 props: true没有动态路由时,你要为 100 个学生写 100 个路由,显然不可能。动态路由用 :变量 占个位,一个规则匹配所有学生:
// 一条规则,匹配 /student/1、/student/2、/student/3……
{ path: '/student/:id', component: StudentDetail }
那详情页怎么拿到这个 3?两种办法:
// 办法 A:组件里自己从路由对象读(麻烦,且耦合路由)
const route = useRoute()
const id = route.params.id
// 办法 B:props: true,让参数直接变成组件的 props(推荐)
// 路由表里写:
{ path: '/student/:id', component: StudentDetail, props: true }
// 组件里写:
const props = defineProps({ id: String }) // 直接用,干净
这一步为什么推荐 props: true:页面组件本来就该“只关心自己要什么数据”,不应该知道“数据是从哪个 URL 来的”。props: true 把“取参数”这件事交给路由,组件只管收 props,更好复用、更好测试。
注意,我说的是内存参数消失,不是路径参数。本课 App.vue 里有一个按钮叫“内存传参进入”,它故意不写进 URL,只把 id 存在 JS 变量里。这样:
id,于是“回到首页 / 找不到”。而“路径参数”因为写在 URL 里(#/student/3),刷新后浏览器把整条 URL 交回来,自然能重新解析出 id=3。记住这条:判断一个参数刷新丢不丢,就看它有没有在 URL 里。
:id 写成了 :id?(可选)或拼错成 :Id,导致解析不到。route.params.id,却忘了路由表里其实没配 :id,拿到 undefined。props: true 会把 query 也变成 props——不会,它只把 params 变 props,query 仍要从 route.query 读。本课
App.vue为了“复制即跑”,手写了十几行 hash 路由。但正式项目请用官方vue-router,下面给你完整骨架。
安装:
npm install vue-router@4
src/main.js 引入并使用:
import { createApp } from 'vue'
import App from './App.vue'
import router from './router' // 引入路由实例
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
createApp(App).use(router).use(ElementPlus).mount('#app')
src/router/index.js(完整):
import { createRouter, createWebHashHistory } from 'vue-router'
// createWebHashHistory 就是“用 # 后面的部分当路由”,和本课手写的一致
const routes = [
{ path: '/', name: 'Home', component: () => import('@/views/Home.vue') },
// props: true —— 把路径参数 id 变成组件 props
{ path: '/student/:id', name: 'StudentDetail', component: () => import('@/views/StudentDetail.vue'), props: true }
]
const router = createRouter({
history: createWebHashHistory(),
routes
})
export default router
StudentDetail.vue 里收参数(干净写法):
<script setup>
const props = defineProps({ id: String }) // 直接拿到路径里的 id
import { useRoute } from 'vue-router'
const route = useRoute()
const tab = route.query.tab // query 仍从这里读
</script>
对比一下你就明白本课在模拟什么:本课手写的 parseHash() 几十行,正是 vue-router 帮你做的事——解析 URL、匹配路由、把 params 喂给组件。学会本课的原理,再去看 vue-router 文档会非常轻松。
来源:本课自编 Vue 3 前端核心技能实训 · 语法依据 Vue 3.5 官方文档 · 组件依据 Element Plus 2.8 官方文档
<script setup>
import { ref, computed, h, onMounted, onBeforeUnmount } from 'vue'
import {
ElConfigProvider, ElCard, ElButton, ElAlert, ElTag, ElDescriptions,
ElDescriptionsItem, ElDivider, ElMessage, ElIcon
} from 'element-plus'
import { ArrowLeft } from '@element-plus/icons-vue'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import 'element-plus/dist/index.css'
/* ==================== 假数据:学生列表 ==================== */
const students = [
{ id: 1, name: '张三', class: '一班', score: 88 },
{ id: 2, name: '李四', class: '二班', score: 92 },
{ id: 3, name: '王五', class: '一班', score: 75 },
{ id: 4, name: '赵六', class: '三班', score: 81 }
]
/* ==================== 极简 hash 路由(这十几行就是 vue-router 帮你做的事) ==================== */
// 路由表::id 是“动态片段”,代表不同学生的 id
const routes = [
{ path: '/', component: 'Home', meta: { title: '学生列表' } },
{ path: '/student/:id', component: 'StudentDetail', meta: { title: '学生详情' } }
]
// 解析 '#/student/3?tab=score' => { path, params, query }
function parseHash() {
const raw = location.hash.slice(1) || '/'
const qIndex = raw.indexOf('?')
const path = qIndex === -1 ? raw : raw.slice(0, qIndex)
const query = {}
if (qIndex !== -1) {
raw.slice(qIndex + 1).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
})
}
// 先精确匹配,再匹配动态路由
let matched = routes.find(r => r.path === path)
let params = {}
if (!matched) {
for (const r of routes) {
const pp = r.path.split('/')
const ap = path.split('/')
if (pp.length !== ap.length) continue
const pm = {}
let ok = true
for (let i = 0; i < pp.length; i++) {
if (pp[i].startsWith(':')) pm[pp[i].slice(1)] = decodeURIComponent(ap[i])
else if (pp[i] !== ap[i]) { ok = false; break }
}
if (ok) { matched = r; params = pm; break }
}
}
if (!matched) matched = { component: 'NotFound', meta: { title: '页面不存在' } }
return { path, component: matched.component, params, query, meta: matched.meta || {} }
}
const currentRoute = ref(parseHash())
const currentHash = ref(location.hash || '(空)')
function applyRoute() {
currentRoute.value = parseHash()
currentHash.value = location.hash || '(空)'
document.title = (currentRoute.value.meta && currentRoute.value.meta.title)
? currentRoute.value.meta.title + ' · 极简路由演示'
: '极简路由演示'
}
onMounted(() => window.addEventListener('hashchange', applyRoute))
onBeforeUnmount(() => window.removeEventListener('hashchange', applyRoute))
/* ==================== 两种“导航”写法(对应 router.push) ==================== */
// 路径(字符串)写法:参数直接写在 URL 里
function goByPath(id) {
location.hash = '#/student/' + id
}
// 对象写法:把 query 单独拆出来(对应 router.push({ path, query }))
function goByObject(id, tab) {
location.hash = '#/student/' + id + '?tab=' + (tab || 'info')
}
/* ==================== “内存参数”演示:刷新会丢失 ==================== */
// 真实 vue-router 里,用 push({ name:'StudentDetail', params:{ id } }) 但路径没占位,刷新就没了
const memoryId = ref(null)
function goByMemory(id) {
memoryId.value = id
// 故意不改 location.hash —— 地址栏没有这个 id
currentRoute.value = {
path: '/student/' + id, component: 'StudentDetail',
params: { id: String(id) }, query: {}, meta: { title: '学生详情' }
}
}
/* ==================== 页面组件(用 h 写,保证即贴即跑,不依赖运行时编译器) ==================== */
const HomePage = {
setup() {
return () => h(ElCard, { class: 'block' }, {
header: () => '学生列表(首页 / )',
default: () => [
h('p', { class: 'muted' }, '点不同按钮,用“路径写法 / 对象写法 / 内存传参”三种方式进入同一名学生详情,观察地址栏与刷新后的表现。'),
h('div', { class: 'btn-row' },
students.map(s => h(ElButton, { key: 'p' + s.id, type: 'primary', plain: true, onClick: () => goByPath(s.id) }, () => '路径写法 → ' + s.name))
),
h(ElDivider, { contentPosition: 'left' }, () => '对象写法(带 query: tab=score)'),
h('div', { class: 'btn-row' },
students.map(s => h(ElButton, { key: 'o' + s.id, onClick: () => goByObject(s.id, 'score') }, () => '对象写法 → ' + s.name))
),
h(ElDivider, { contentPosition: 'left' }, '内存传参(刷新会丢)'),
h(ElButton, { type: 'warning', plain: true, onClick: () => goByMemory(students[0].id) }, '内存传参进入 ' + students[0].name)
]
})
}
}
const StudentDetailPage = {
// props: true 的效果 —— 动态片段 :id 作为 prop 传入
props: { id: { type: String, default: '' } },
setup(props) {
const student = computed(() => students.find(s => String(s.id) === props.id) || null)
// query 不是 prop,要从当前路由里读(对应 route.query)
const tab = computed(() => currentRoute.value.query.tab || 'info')
const tabLabel = computed(() => tab.value === 'score' ? '成绩' : '基本资料')
return () => {
if (!student.value) {
return h(ElAlert, { type: 'error', title: '找不到 id=' + props.id + ' 的学生', closable: false, showIcon: true })
}
return h(ElCard, { class: 'block' }, {
header: () => h('span', [
h(ElIcon, { style: 'margin-right:6px' }, () => h(ArrowLeft)),
'学生详情 路径: #/student/' + props.id + ' 查询: tab=' + tab.value
]),
default: () => [
h(ElDescriptions, { column: 1, border: true, size: 'small' }, {
default: () => [
h(ElDescriptionsItem, { label: 'ID(来自路径参数 :id,作为 props 传入)' }, () => String(student.value.id)),
h(ElDescriptionsItem, { label: '姓名' }, () => student.value.name),
h(ElDescriptionsItem, { label: '班级' }, () => student.value.class),
h(ElDescriptionsItem, { label: '当前 tab(来自 query,需用 route.query 读取)' }, () => tabLabel.value),
tab.value === 'score'
? h(ElDescriptionsItem, { label: '成绩' }, () => String(student.value.score))
: null
].filter(Boolean)
}),
h('p', { class: 'muted', style: 'margin-top:12px' }, '注意:id 是通过 props 拿到的(对应 vue-router 的 props: true);tab 不在 URL 路径里,只能从 query 读。')
]
})
}
}
}
const NotFoundPage = {
setup() {
return () => h(ElAlert, { type: 'warning', title: '页面不存在(路径没有匹配到任何路由)', closable: false, showIcon: true })
}
}
const pageComponent = computed(() => {
switch (currentRoute.value.component) {
case 'Home': return HomePage
case 'StudentDetail': return StudentDetailPage
default: return NotFoundPage
}
})
// 模拟 props: true —— 只把 params 作为 props 传给组件(query 不进 props)
const pageProps = computed(() => ({ ...currentRoute.value.params }))
const routeInfo = computed(() => ({
hash: currentHash.value,
path: currentRoute.value.path,
params: JSON.stringify(currentRoute.value.params),
query: JSON.stringify(currentRoute.value.query)
}))
function backHome() {
location.hash = '#/'
}
</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>解析结果:path = <b>{{ routeInfo.path }}</b> ,params = <b>{{ routeInfo.params }}</b> ,query = <b>{{ routeInfo.query }}</b></div>
<div class="muted">把页面刷新一下(F5),看看哪些参数还在、哪些丢了。</div>
</template>
</el-alert>
<component :is="pageComponent" v-bind="pageProps" />
<div style="margin-top:16px">
<el-button @click="backHome">返回首页(#/)</el-button>
</div>
<el-alert
title="要点:① query 写在 ? 后面,刷新不丢;② params 分两种——路径里的 :id 刷新不丢,内存里的 params 刷新丢;③ props:true 让组件用 props 接收路径参数。"
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: 980px; 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; }
</style>
#/student/1,下方显示 1 号学生的资料。#/student/2?tab=score,详情页“当前 tab”显示“成绩”,并多出一行成绩 92。props,tab 来自 query,两者来源不同,但都在页面正确显示。#/abc,显示“页面不存在”警告。为什么“内存传参进入”的学生,刷新后就回不去了,而“路径写法”的学生刷新还在?
因为“内存参数”只存在 JS 的运行内存里,没写进 URL。 刷新时浏览器重新加载页面,所有 JS 变量清零,而 URL(#/student/1)里本就带着 id,所以手写的 parseHash() 能从地址栏重新解析出 id=1,自然还在;内存传参的 id 既不在 URL 里、刷新后又被清零,当然就丢了。这个区别的实际意义是:凡是你希望“用户刷新 / 收藏 / 分享链接后还能看到同样内容”的参数,都必须放进 URL(路径参数或 query),不要只放内存。
来源:本课自编 Vue 3 前端核心技能实训 · 语法依据 Vue 3.5 官方文档 · 组件依据 Element Plus 2.8 官方文档
const routes = [
{ path: '/', component: 'Home' },
{ path: '/student/:id', component: 'StudentDetail' }
]
预期:先不接参数,只验证“改 location.hash 能切换组件”。这一步让你确认 <component :is> 是根据 currentRoute.component 变的。
写 parseHash(),把 #/student/3?tab=score 拆成 path、动态 params、query。预期:在首页敲地址栏改成 #/student/2,下方解析面板里 params 出现 { "id": "2" }。
给 StudentDetailPage 加 props:['id'],并用 v-bind="pageProps" 把 params 传进去。预期:详情页不再自己读 URL,而是声明“我要一个 id 的 prop”。这一步对应正式项目的 props: true。
在详情页用 currentRoute.value.query.tab 读 tab。预期:访问带 ?tab=score 的地址,页面显示成绩,否则显示基本资料。
function goByMemory(id) {
memoryId.value = id
currentRoute.value = { /* 只有 params,没动 location.hash */ }
}
预期:页面切换了,但地址栏没变。然后按 F5 刷新,验证它丢了——这是本课最重要的“啊哈”时刻。
hashchange 监听与 document.titleonMounted(() => window.addEventListener('hashchange', applyRoute))
预期:手动在浏览器地址栏改 #/student/3,页面自动跟着变(不用点按钮)。document.title 也跟着变成“学生详情”。
把第 3 页的完整 App.vue 覆盖 src/App.vue,把七条“预期看见什么”全走一遍,尤其别忘了 F5 那两条。
| 位置 | 本次作用 |
|---|---|
routes |
路由表,/student/:id 是动态路由。 |
parseHash |
解析 URL,匹配动态片段,产出 params / query。 |
currentRoute |
当前路由状态(响应式)。 |
goByPath / goByObject |
两种“跳转”写法(路径 / 对象)。 |
goByMemory |
演示“内存参数刷新丢”。 |
pageComponent / pageProps |
用 <component :is> 渲染对应页面,并把 params 当 props。 |
StudentDetailPage 的 props |
对应 props: true,用 props 收 id。 |
| 操作或输入 | 预期观察 |
|---|---|
| 点“路径写法”进详情 | 地址栏变成 #/student/x,刷新仍在。 |
| 点“对象写法”进详情 | 地址栏带 ?tab=score,显示成绩。 |
| 点“内存传参”后进详情 | 地址栏不变,F5 后退回首页。 |
手动改地址栏为 #/student/2 |
页面自动变为 2 号学生(hashchange 生效)。 |
访问 #/abc |
显示“页面不存在”。 |
| 详情页 ID 来源 | 来自 props.id,不是组件自己读 URL。 |
来源:本课自编 Vue 3 前端核心技能实训 · 语法依据 Vue 3.5 官方文档 · 组件依据 Element Plus 2.8 官方文档
/product/:id 显示名称、价格、库存。/product/5?from=home,详情页根据 from 显示“来自首页推荐”或“来自搜索”,并且把它显示在页面上。ref 记住上次看的 id,返回首页时高亮它)。location.href)复制到剪贴板(navigator.clipboard.writeText),并提示“已复制,刷新也不会丢”(前提是你用的是路径/ query 参数,不是内存参数)。src/router/index.js,把你手写的 routes 改成 createRouter 的写法,看看和本课的差异。| 步骤 | 改哪里 | 运行后应该看到什么 |
|---|---|---|
| 1 换业务 | students → 你的数据、routes 路径 |
自己的列表与详情。 |
| 2 加 query | 详情页读 query.from |
不同 from 显示不同文案。 |
| 3 记位置 | 列表页 ref + 高亮 |
返回后刚才的商品被高亮。 |
| 4 分享 | 复制 location.href |
提示已复制,刷新不丢。 |
| 5 正规化 | router/index.js |
路由表用 createRouter 表达。 |
| 检查项 | 通过条件 |
|---|---|
| 动态路由生效 | 访问 /:id 形式能显示对应数据。 |
| query 与 params 分得清 | 能说清哪个在 ? 后、哪个在路径里。 |
| props 接收参数 | 详情页用 props 拿到 id,而非自己读 URL。 |
parseHash() 函数,标出“哪一行在解析 params、哪一行在解析 query”。props: true 对你有什么好处。提交 src/App.vue 与 练习记录.md。
来源:本课自编 Vue 3 前端核心技能实训 · 语法依据 Vue 3.5 官方文档 · 组件依据 Element Plus 2.8 官方文档
| 看到什么现象 / 报错 | 先检查哪里 | 修好后怎样确认 |
|---|---|---|
详情页显示 undefined 的 id |
路由表没写 :id,或组件没声明 props |
路由加 :id、组件加 props:['id'] 后正常。 |
| 刷新后详情变回首页 | 用的是“内存参数”(没写进 URL) | 改成路径参数或 query 后刷新仍在。 |
| 点按钮不跳转 | 只改了 currentRoute,没改 location.hash(或反之逻辑错乱) |
统一用 location.hash = '...' 跳转后正常。 |
| query 取不到 | 以为 props 里有,其实 query 不在 props 里 |
从 route.query / currentRoute.query 读。 |
| 动态路由匹配不到 | :id 写成了别的占位或路径层级数对不上 |
路径层级一致、占位名正确后匹配。 |
| 改地址栏页面没变 | 没监听 hashchange,或组件没 onMounted 注册 |
注册监听后手动改地址栏能切换。 |
解析出 params 是乱码 |
没 decodeURIComponent(参数含中文/特殊符) |
解码后中文正常。 |
| 页面切了但标题没变 | 忘了在 applyRoute 里改 document.title |
加上后标题跟随变化。 |
id 是 undefined,先看路由表有没有 :id; 刷新就回首页,是参数没写进 URL; query 取不到,别去 props 里找; 地址栏改了不动,是没监听 hashchange。
localStorage 里乱用。真正该进 URL 的参数就老老实实写进路径或 query,这才符合“可分享、可收藏”的网页语义。parseHash 里用 eval 或字符串拼接去“执行”路径。路径匹配用上面这种逐段比较就够了,别引入安全和性能隐患。props: true 当成“query 也能变 props”。它只映射 params,query 永远要从 route.query 读——混淆两者会让你取不到值还不知道为什么。你的 routes 路由表、你访问的完整 URL(含 #)、你期望的 params/query、你实际解析出来的结果截图、详情页组件里收 id 的写法。
#/student/3?tab=score 里,哪个是 params、哪个是 query?(能指出 3 是 params、tab=score 是 query)id 是从 props 拿的还是自己读 URL 拿的?(推荐答“props”,即 props: true 的思想)来源:本课自编 Vue 3 前端核心技能实训 · 语法依据 Vue 3.5 官方文档 · 组件依据 Element Plus 2.8 官方文档
为什么“内存传参”刷新就回不去,而“路径写法”刷新还在?
参考解释:内存参数只活在 JS 运行期,URL 里没有它,刷新后变量清零就消失了;路径参数写在 URL 中,刷新时浏览器把整条地址交回,解析函数重新从 #/student/1 里取出 id=1,所以还在。结论:要持久、要能分享的参数必须进 URL。
| 问题 | 参考解释 |
|---|---|
| query 和 params 到底该怎么选? | 同一页面不同内容用路径参数(动态路由 :id);“可分享、可省略”的附加信息用 query(?tab=score)。两者刷新都不丢。 |
props: true 好在哪? |
它把“取路由参数”交给路由,组件只声明自己要什么 props,解耦、好复用、好测试。 |
| 考考自己 | 可以这样通俗回答 |
|---|---|
动态路由 :id 是什么? |
路由里的一个“变量坑位”,一条规则能匹配 /student/1、/student/2 一大批 URL。 |
| 怎么让组件拿到路径参数? | 路由写 props: true,组件 defineProps(['id'])。 |
| query 存在哪? | 在 URL 的 ? 后面,用 route.query 读,不进 props。 |
| 为什么内存参数刷新会丢? | 因为它没写进 URL,只存在 JS 内存,刷新即清零。 |
<component :is> 在本课扮演什么? |
它就是“根据当前路由切换显示哪个页面”的开关,正是 vue-router 的 <router-view> 在做的事。 |
#/student/2,页面显示 2 号学生,说“一条规则管所有学生”。#/student/2?tab=score,指出“成绩”出现,说明 query 是附加信息。src/App.vue 与 练习记录.md 已保存。/student/:id 能显示不同学生。id。把本课手写的 parseHash() 和正式 vue-router 的 createRouter 对照着读一遍:找出“哪几行在手写代码里、对应 vue-router 的哪个概念”。写下一句话:理解了手写路由之后,再看 vue-router 文档,你觉得哪一步它替你省了事?
来源:本课自编 Vue 3 前端核心技能实训 · 语法依据 Vue 3.5 官方文档 · 组件依据 Element Plus 2.8 官方文档