2026-02-06 03:34:50 +08:00
|
|
|
|
<template>
|
|
|
|
|
|
<div class="nested-routes-demo">
|
|
|
|
|
|
<div class="demo-header">
|
2026-02-13 22:10:03 +08:00
|
|
|
|
<span class="icon">🪆</span>
|
|
|
|
|
|
<span class="title">嵌套路由</span>
|
|
|
|
|
|
<span class="subtitle">层层嵌套的视图容器</span>
|
2026-02-06 03:34:50 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
<div class="intro-text">
|
|
|
|
|
|
想象<span class="highlight">俄罗斯套娃</span>:每个大娃娃里都有小娃娃,小娃娃里还有更小的。嵌套路由就是这样,父组件的<span class="highlight">RouterView</span>里可以渲染子组件,一层套一层。
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div class="demo-content">
|
2026-02-06 03:34:50 +08:00
|
|
|
|
<!-- 路由层级可视化 -->
|
|
|
|
|
|
<div class="routes-hierarchy">
|
|
|
|
|
|
<div class="tree-view">
|
|
|
|
|
|
<div
|
|
|
|
|
|
v-for="node in treeData"
|
|
|
|
|
|
:key="node.path"
|
|
|
|
|
|
class="tree-node"
|
2026-02-13 22:10:03 +08:00
|
|
|
|
:style="{ paddingLeft: `${node.level * 20}px` }"
|
2026-02-06 03:34:50 +08:00
|
|
|
|
@click="selectNode(node)"
|
|
|
|
|
|
>
|
|
|
|
|
|
<div
|
|
|
|
|
|
:class="[
|
|
|
|
|
|
'node-content',
|
2026-02-13 22:10:03 +08:00
|
|
|
|
{ active: currentPath === node.path }
|
2026-02-06 03:34:50 +08:00
|
|
|
|
]"
|
|
|
|
|
|
>
|
|
|
|
|
|
<span class="node-icon">{{ node.children?.length ? '📁' : '📄' }}</span>
|
2026-02-13 22:10:03 +08:00
|
|
|
|
<span class="node-name">{{ node.name }}</span>
|
2026-02-06 03:34:50 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<!-- 渲染区域预览 -->
|
|
|
|
|
|
<div class="render-preview">
|
|
|
|
|
|
<div class="preview-header">
|
2026-02-13 22:10:03 +08:00
|
|
|
|
<h5>🔲 渲染视图</h5>
|
2026-02-06 03:34:50 +08:00
|
|
|
|
<span class="current-path">{{ currentPath || '/' }}</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div class="router-view-hierarchy">
|
|
|
|
|
|
<div
|
|
|
|
|
|
v-for="(route, index) in activeRouteChain"
|
|
|
|
|
|
:key="route.path"
|
|
|
|
|
|
class="router-view-level"
|
2026-02-13 22:10:03 +08:00
|
|
|
|
:style="{ marginLeft: `${index * 16}px` }"
|
2026-02-06 03:34:50 +08:00
|
|
|
|
>
|
|
|
|
|
|
<div class="router-view-box">
|
|
|
|
|
|
<div class="view-label">
|
2026-02-13 22:10:03 +08:00
|
|
|
|
<span class="view-icon">📦</span>
|
2026-02-06 03:34:50 +08:00
|
|
|
|
<span class="view-name">{{ route.name }}</span>
|
|
|
|
|
|
</div>
|
2026-02-13 22:10:03 +08:00
|
|
|
|
<div class="view-path">{{ route.path || '/' }}</div>
|
2026-02-06 03:34:50 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div class="breadcrumb">
|
|
|
|
|
|
<span
|
|
|
|
|
|
v-for="(crumb, index) in breadcrumbs"
|
|
|
|
|
|
:key="index"
|
|
|
|
|
|
class="breadcrumb-item"
|
|
|
|
|
|
@click="navigateTo(crumb.path)"
|
|
|
|
|
|
>
|
|
|
|
|
|
{{ crumb.name }}
|
|
|
|
|
|
<span v-if="index < breadcrumbs.length - 1" class="separator">/</span>
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
<div class="info-box">
|
|
|
|
|
|
<span class="icon">💡</span>
|
|
|
|
|
|
<strong>核心概念:</strong>嵌套路由通过在父组件中放置 RouterView 来实现子路由的渲染。每个路由层级都有自己的 RouterView,就像套娃一样一层层展示。
|
2026-02-06 03:34:50 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script setup>
|
|
|
|
|
|
import { ref, computed } from 'vue'
|
|
|
|
|
|
|
|
|
|
|
|
const currentPath = ref('/dashboard')
|
|
|
|
|
|
|
|
|
|
|
|
const routeConfig = [
|
|
|
|
|
|
{
|
|
|
|
|
|
path: '/',
|
|
|
|
|
|
name: 'Layout',
|
|
|
|
|
|
component: 'Layout',
|
|
|
|
|
|
children: [
|
|
|
|
|
|
{
|
|
|
|
|
|
path: '',
|
|
|
|
|
|
name: 'Home',
|
|
|
|
|
|
component: 'Home'
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
path: 'dashboard',
|
|
|
|
|
|
name: 'Dashboard',
|
|
|
|
|
|
component: 'Dashboard'
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
path: 'users',
|
|
|
|
|
|
name: 'Users',
|
|
|
|
|
|
component: 'UserLayout',
|
|
|
|
|
|
children: [
|
|
|
|
|
|
{
|
|
|
|
|
|
path: '',
|
|
|
|
|
|
name: 'UserList',
|
|
|
|
|
|
component: 'UserList'
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
path: ':id',
|
|
|
|
|
|
name: 'UserDetail',
|
|
|
|
|
|
component: 'UserDetail'
|
|
|
|
|
|
}
|
|
|
|
|
|
]
|
|
|
|
|
|
}
|
|
|
|
|
|
]
|
|
|
|
|
|
}
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
const flattenRoutes = (routes, level = 0, parentPath = '') => {
|
|
|
|
|
|
const result = []
|
|
|
|
|
|
routes.forEach(route => {
|
|
|
|
|
|
const fullPath = route.path
|
|
|
|
|
|
? `${parentPath}/${route.path}`.replace(/\/+/g, '/')
|
|
|
|
|
|
: parentPath || '/'
|
|
|
|
|
|
const node = {
|
|
|
|
|
|
...route,
|
|
|
|
|
|
fullPath,
|
|
|
|
|
|
level,
|
|
|
|
|
|
children: []
|
|
|
|
|
|
}
|
|
|
|
|
|
if (route.children?.length) {
|
|
|
|
|
|
node.children = flattenRoutes(route.children, level + 1, fullPath)
|
|
|
|
|
|
}
|
|
|
|
|
|
result.push(node)
|
|
|
|
|
|
})
|
|
|
|
|
|
return result
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const treeData = computed(() => {
|
|
|
|
|
|
const flatten = (routes, level = 0) => {
|
|
|
|
|
|
const result = []
|
|
|
|
|
|
routes.forEach(route => {
|
|
|
|
|
|
const node = {
|
|
|
|
|
|
name: route.name,
|
|
|
|
|
|
path: route.path || '/',
|
|
|
|
|
|
fullPath: route.fullPath,
|
|
|
|
|
|
level,
|
|
|
|
|
|
component: route.component,
|
|
|
|
|
|
children: route.children?.length ? flatten(route.children, level + 1) : null
|
|
|
|
|
|
}
|
|
|
|
|
|
result.push(node)
|
|
|
|
|
|
})
|
|
|
|
|
|
return result
|
|
|
|
|
|
}
|
|
|
|
|
|
return flatten(flattenRoutes(routeConfig))
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const activeRouteChain = computed(() => {
|
|
|
|
|
|
const findChain = (routes, target, chain = []) => {
|
|
|
|
|
|
for (const route of routes) {
|
|
|
|
|
|
const currentChain = [...chain, route]
|
|
|
|
|
|
if (route.path === target || route.fullPath === target) {
|
|
|
|
|
|
return currentChain
|
|
|
|
|
|
}
|
|
|
|
|
|
if (route.children?.length) {
|
|
|
|
|
|
const found = findChain(route.children, target, currentChain)
|
|
|
|
|
|
if (found) return found
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return null
|
|
|
|
|
|
}
|
|
|
|
|
|
return findChain(flattenRoutes(routeConfig), currentPath.value) || []
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const breadcrumbs = computed(() => {
|
|
|
|
|
|
return activeRouteChain.value.map(route => ({
|
|
|
|
|
|
name: route.name,
|
|
|
|
|
|
path: route.fullPath || route.path
|
|
|
|
|
|
}))
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const selectNode = (node) => {
|
|
|
|
|
|
currentPath.value = node.fullPath || node.path
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const navigateTo = (path) => {
|
|
|
|
|
|
currentPath.value = path
|
|
|
|
|
|
}
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
|
.nested-routes-demo {
|
2026-02-13 22:10:03 +08:00
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
border-radius: 8px;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
background: var(--vp-c-bg-soft);
|
2026-02-13 22:10:03 +08:00
|
|
|
|
padding: 1rem;
|
|
|
|
|
|
margin: 1rem 0;
|
|
|
|
|
|
max-height: 600px;
|
|
|
|
|
|
overflow-y: auto;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.demo-header {
|
2026-02-13 22:10:03 +08:00
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
gap: 0.5rem;
|
|
|
|
|
|
margin-bottom: 0.75rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.demo-header .icon { font-size: 1.25rem; }
|
|
|
|
|
|
.demo-header .title { font-weight: bold; font-size: 1rem; }
|
|
|
|
|
|
.demo-header .subtitle { color: var(--vp-c-text-2); font-size: 0.85rem; margin-left: 0.5rem; }
|
2026-02-06 03:34:50 +08:00
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.intro-text {
|
|
|
|
|
|
font-size: 0.9rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
color: var(--vp-c-text-2);
|
2026-02-13 22:10:03 +08:00
|
|
|
|
line-height: 1.6;
|
|
|
|
|
|
margin-bottom: 1rem;
|
|
|
|
|
|
padding: 0.75rem;
|
|
|
|
|
|
background: var(--vp-c-bg);
|
|
|
|
|
|
border-radius: 6px;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.intro-text .highlight {
|
|
|
|
|
|
color: var(--vp-c-brand-1);
|
|
|
|
|
|
font-weight: 500;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.demo-content {
|
2026-02-06 03:34:50 +08:00
|
|
|
|
display: grid;
|
|
|
|
|
|
grid-template-columns: 1fr 1fr;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
gap: 1rem;
|
|
|
|
|
|
margin-bottom: 1rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.routes-hierarchy {
|
|
|
|
|
|
background: var(--vp-c-bg);
|
|
|
|
|
|
border-radius: 8px;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
padding: 0.75rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.tree-view {
|
2026-02-13 22:10:03 +08:00
|
|
|
|
max-height: 280px;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
overflow-y: auto;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.tree-node {
|
|
|
|
|
|
margin: 2px 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.node-content {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
gap: 0.5rem;
|
|
|
|
|
|
padding: 0.5rem 0.75rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
border-radius: 6px;
|
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
|
transition: all 0.2s;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.node-content:hover {
|
|
|
|
|
|
background: var(--vp-c-bg-soft);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.node-content.active {
|
|
|
|
|
|
background: var(--vp-c-brand-soft);
|
|
|
|
|
|
border: 1px solid var(--vp-c-brand);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.node-icon {
|
2026-02-13 22:10:03 +08:00
|
|
|
|
font-size: 0.85rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.node-name {
|
|
|
|
|
|
font-size: 0.8rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
font-weight: 500;
|
|
|
|
|
|
color: var(--vp-c-text-1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.render-preview {
|
|
|
|
|
|
background: var(--vp-c-bg);
|
|
|
|
|
|
border-radius: 8px;
|
|
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
overflow: hidden;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.preview-header {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
justify-content: space-between;
|
|
|
|
|
|
align-items: center;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
padding: 0.75rem 1rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
background: var(--vp-c-bg-soft);
|
|
|
|
|
|
border-bottom: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.preview-header h5 {
|
|
|
|
|
|
margin: 0;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
font-size: 0.85rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
color: var(--vp-c-text-1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.current-path {
|
2026-02-13 22:10:03 +08:00
|
|
|
|
font-size: 0.75rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
color: var(--vp-c-text-3);
|
|
|
|
|
|
font-family: monospace;
|
|
|
|
|
|
background: var(--vp-c-bg);
|
2026-02-13 22:10:03 +08:00
|
|
|
|
padding: 0.125rem 0.5rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
border-radius: 4px;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.router-view-hierarchy {
|
2026-02-13 22:10:03 +08:00
|
|
|
|
padding: 1rem;
|
|
|
|
|
|
min-height: 180px;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.router-view-level {
|
2026-02-13 22:10:03 +08:00
|
|
|
|
margin-bottom: 0.5rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.router-view-box {
|
|
|
|
|
|
background: var(--vp-c-bg-soft);
|
|
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
border-radius: 6px;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
padding: 0.5rem 0.75rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.view-label {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
gap: 0.375rem;
|
|
|
|
|
|
margin-bottom: 0.25rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.view-icon {
|
2026-02-13 22:10:03 +08:00
|
|
|
|
font-size: 0.75rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.view-name {
|
2026-02-13 22:10:03 +08:00
|
|
|
|
font-size: 0.8rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
font-weight: 500;
|
|
|
|
|
|
color: var(--vp-c-text-1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.view-path {
|
2026-02-13 22:10:03 +08:00
|
|
|
|
font-size: 0.7rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
color: var(--vp-c-text-3);
|
|
|
|
|
|
font-family: monospace;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.breadcrumb {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
gap: 0.25rem;
|
|
|
|
|
|
padding: 0.75rem 1rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
background: var(--vp-c-bg-soft);
|
|
|
|
|
|
border-top: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
overflow-x: auto;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.breadcrumb-item {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
gap: 0.25rem;
|
|
|
|
|
|
font-size: 0.75rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
color: var(--vp-c-text-2);
|
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
|
white-space: nowrap;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.breadcrumb-item:hover {
|
|
|
|
|
|
color: var(--vp-c-brand);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.separator {
|
|
|
|
|
|
color: var(--vp-c-text-3);
|
2026-02-13 22:10:03 +08:00
|
|
|
|
margin: 0 0.125rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.info-box {
|
|
|
|
|
|
background: var(--vp-c-bg-alt);
|
|
|
|
|
|
padding: 0.75rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
border-radius: 6px;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
font-size: 0.85rem;
|
|
|
|
|
|
color: var(--vp-c-text-2);
|
|
|
|
|
|
margin-top: 1rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.info-box .icon { margin-right: 0.25rem; }
|
|
|
|
|
|
|
2026-02-06 03:34:50 +08:00
|
|
|
|
@media (max-width: 768px) {
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.demo-content {
|
2026-02-06 03:34:50 +08:00
|
|
|
|
grid-template-columns: 1fr;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.breadcrumb {
|
|
|
|
|
|
flex-wrap: wrap;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
</style>
|