2026-02-06 03:34:50 +08:00
|
|
|
|
<template>
|
|
|
|
|
|
<div class="route-matching-demo">
|
|
|
|
|
|
<div class="demo-header">
|
2026-02-13 22:10:03 +08:00
|
|
|
|
<span class="icon">🎯</span>
|
|
|
|
|
|
<span class="title">路由匹配</span>
|
|
|
|
|
|
<span class="subtitle">URL如何找到对应组件</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>:输入一个词,字典会帮你找到对应的解释。路由匹配也是这样,浏览器根据URL路径,在路由配置中找到最匹配的那一项,然后渲染对应组件。
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div class="demo-content">
|
2026-02-06 03:34:50 +08:00
|
|
|
|
<div class="input-section">
|
2026-02-13 22:10:03 +08:00
|
|
|
|
<h5>📍 测试路径</h5>
|
2026-02-06 03:34:50 +08:00
|
|
|
|
<div class="input-group">
|
2026-02-13 22:10:03 +08:00
|
|
|
|
<span class="input-prefix">/</span>
|
|
|
|
|
|
<input
|
|
|
|
|
|
v-model="testPath"
|
|
|
|
|
|
type="text"
|
|
|
|
|
|
placeholder="user/123"
|
|
|
|
|
|
class="path-input"
|
|
|
|
|
|
@input="testMatch"
|
|
|
|
|
|
>
|
2026-02-06 03:34:50 +08:00
|
|
|
|
</div>
|
2026-02-13 22:10:03 +08:00
|
|
|
|
<div class="hint-text">试试:user/123 或 products/electronics/456</div>
|
2026-02-06 03:34:50 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
<div class="result-section">
|
|
|
|
|
|
<h5>🎯 匹配结果</h5>
|
|
|
|
|
|
<div v-if="matchResult && matchResult.matched" class="match-success">
|
|
|
|
|
|
<div class="success-icon">✅</div>
|
|
|
|
|
|
<div class="result-details">
|
|
|
|
|
|
<div class="result-row">
|
|
|
|
|
|
<span class="label">匹配路由:</span>
|
|
|
|
|
|
<code class="value">{{ matchResult.route.path }}</code>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div v-if="Object.keys(matchResult.params).length" class="params-box">
|
|
|
|
|
|
<span class="label">提取参数:</span>
|
|
|
|
|
|
<div class="params-list">
|
|
|
|
|
|
<span v-for="(value, key) in matchResult.params" :key="key" class="param-tag">
|
|
|
|
|
|
{{ key }} = {{ value }}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</div>
|
2026-02-06 03:34:50 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2026-02-13 22:10:03 +08:00
|
|
|
|
<div v-else class="match-fail">
|
|
|
|
|
|
<div class="fail-icon">❌</div>
|
|
|
|
|
|
<div>未找到匹配的路由</div>
|
|
|
|
|
|
</div>
|
2026-02-06 03:34:50 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
<div class="routes-list">
|
|
|
|
|
|
<h5>📋 已定义的路由</h5>
|
|
|
|
|
|
<div class="routes-grid">
|
|
|
|
|
|
<div
|
|
|
|
|
|
v-for="route in routes"
|
|
|
|
|
|
:key="route.path"
|
|
|
|
|
|
:class="['route-item', { matched: matchedRoute?.path === route.path }]"
|
|
|
|
|
|
>
|
|
|
|
|
|
<code class="route-path">{{ route.path }}</code>
|
|
|
|
|
|
<span class="route-name">{{ route.name }}</span>
|
2026-02-06 03:34:50 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
<div class="info-box">
|
|
|
|
|
|
<span class="icon">💡</span>
|
|
|
|
|
|
<strong>匹配规则:</strong>路由按定义顺序匹配,先定义的优先。动态参数(:id)可以匹配任意值,但精确匹配优先级更高。
|
2026-02-06 03:34:50 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script setup>
|
|
|
|
|
|
import { ref, computed } from 'vue'
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
const testPath = ref('user/123')
|
2026-02-06 03:34:50 +08:00
|
|
|
|
const matchResult = ref(null)
|
|
|
|
|
|
const matchedRoute = ref(null)
|
|
|
|
|
|
|
|
|
|
|
|
const routes = [
|
|
|
|
|
|
{ path: '/', name: '首页', hasParams: false },
|
|
|
|
|
|
{ path: '/user', name: '用户列表', hasParams: false },
|
|
|
|
|
|
{ path: '/user/:id', name: '用户详情', hasParams: true },
|
|
|
|
|
|
{ path: '/user/:id/posts', name: '用户文章', hasParams: true },
|
|
|
|
|
|
{ path: '/products/:category/:id', name: '产品详情', hasParams: true },
|
|
|
|
|
|
{ path: '/:path(.*)*', name: '404页面', hasParams: true }
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
const parsePath = (path) => {
|
|
|
|
|
|
const cleanPath = path.replace(/^\//, '')
|
|
|
|
|
|
return cleanPath.split('/').filter(Boolean)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const matchPath = (routePath, testPath) => {
|
|
|
|
|
|
const routeParts = parsePath(routePath)
|
|
|
|
|
|
const testParts = parsePath(testPath)
|
|
|
|
|
|
const params = {}
|
|
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < routeParts.length; i++) {
|
|
|
|
|
|
const routePart = routeParts[i]
|
|
|
|
|
|
const testPart = testParts[i]
|
|
|
|
|
|
|
|
|
|
|
|
if (routePart === '(.*)*' || routePart === ':path(.*)*') {
|
|
|
|
|
|
params['pathMatch'] = testParts.slice(i).join('/')
|
|
|
|
|
|
return { matched: true, params }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (routePart.startsWith(':')) {
|
|
|
|
|
|
const paramName = routePart.replace(/^:/, '').replace(/\?$/, '')
|
|
|
|
|
|
const isOptional = routePart.endsWith('?')
|
|
|
|
|
|
|
|
|
|
|
|
if (testPart !== undefined) {
|
|
|
|
|
|
params[paramName] = testPart
|
|
|
|
|
|
continue
|
|
|
|
|
|
} else if (isOptional) {
|
|
|
|
|
|
continue
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return { matched: false, params: {} }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (routePart !== testPart) {
|
|
|
|
|
|
return { matched: false, params: {} }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (testParts.length > routeParts.length) {
|
|
|
|
|
|
const lastRoutePart = routeParts[routeParts.length - 1]
|
|
|
|
|
|
if (!lastRoutePart || (!lastRoutePart.includes('*') && !lastRoutePart.endsWith('+'))) {
|
|
|
|
|
|
return { matched: false, params: {} }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return { matched: true, params }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const testMatch = () => {
|
|
|
|
|
|
if (!testPath.value.trim()) {
|
|
|
|
|
|
matchResult.value = { matched: false }
|
|
|
|
|
|
matchedRoute.value = null
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let foundMatch = false
|
|
|
|
|
|
|
|
|
|
|
|
for (const route of routes) {
|
|
|
|
|
|
const { matched, params } = matchPath(route.path, testPath.value)
|
|
|
|
|
|
|
|
|
|
|
|
if (matched) {
|
|
|
|
|
|
matchResult.value = {
|
|
|
|
|
|
matched: true,
|
|
|
|
|
|
route,
|
|
|
|
|
|
params
|
|
|
|
|
|
}
|
|
|
|
|
|
matchedRoute.value = route
|
|
|
|
|
|
foundMatch = true
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!foundMatch) {
|
|
|
|
|
|
matchResult.value = { matched: false }
|
|
|
|
|
|
matchedRoute.value = null
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
testMatch()
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
|
.route-matching-demo {
|
2026-02-13 22:10:03 +08:00
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
2026-02-14 20:23:34 +08:00
|
|
|
|
border-radius: 6px;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
background: var(--vp-c-bg-soft);
|
2026-02-14 20:23:34 +08:00
|
|
|
|
padding: 0.75rem;
|
|
|
|
|
|
margin: 0.5rem 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.intro-text .highlight {
|
|
|
|
|
|
color: var(--vp-c-brand-1);
|
|
|
|
|
|
font-weight: 500;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.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
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.input-section, .result-section {
|
2026-02-06 03:34:50 +08:00
|
|
|
|
background: var(--vp-c-bg);
|
2026-02-14 20:23:34 +08:00
|
|
|
|
border-radius: 6px;
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
h5 {
|
|
|
|
|
|
margin: 0 0 0.5rem 0;
|
|
|
|
|
|
font-size: 0.85rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
color: var(--vp-c-text-2);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.input-group {
|
2026-02-06 03:34:50 +08:00
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
background: var(--vp-c-bg-soft);
|
|
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
border-radius: 6px;
|
|
|
|
|
|
overflow: hidden;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
margin-bottom: 0.5rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.input-prefix {
|
|
|
|
|
|
padding: 0.5rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
color: var(--vp-c-text-3);
|
|
|
|
|
|
font-family: monospace;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
font-size: 0.85rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.path-input {
|
|
|
|
|
|
flex: 1;
|
|
|
|
|
|
border: none;
|
|
|
|
|
|
background: transparent;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
padding: 0.5rem;
|
|
|
|
|
|
font-size: 0.85rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
color: var(--vp-c-text-1);
|
|
|
|
|
|
outline: none;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.hint-text {
|
|
|
|
|
|
font-size: 0.7rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
color: var(--vp-c-text-3);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.match-success {
|
2026-02-06 03:34:50 +08:00
|
|
|
|
display: flex;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
gap: 0.75rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.success-icon {
|
|
|
|
|
|
font-size: 1.5rem;
|
|
|
|
|
|
flex-shrink: 0;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.result-details {
|
|
|
|
|
|
flex: 1;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.result-row {
|
2026-02-06 03:34:50 +08:00
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
gap: 0.5rem;
|
|
|
|
|
|
margin-bottom: 0.5rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.label {
|
|
|
|
|
|
font-size: 0.75rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
color: var(--vp-c-text-3);
|
2026-02-13 22:10:03 +08:00
|
|
|
|
min-width: 60px;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.value {
|
|
|
|
|
|
font-size: 0.8rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
color: var(--vp-c-text-1);
|
2026-02-13 22:10:03 +08:00
|
|
|
|
font-family: monospace;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
background: var(--vp-c-bg-soft);
|
2026-02-13 22:10:03 +08:00
|
|
|
|
padding: 0.25rem 0.5rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
border-radius: 4px;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.params-box {
|
|
|
|
|
|
padding-top: 0.5rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
border-top: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.params-list {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
flex-wrap: wrap;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
gap: 0.5rem;
|
|
|
|
|
|
margin-top: 0.5rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.param-tag {
|
|
|
|
|
|
background: var(--vp-c-brand-soft);
|
|
|
|
|
|
padding: 0.25rem 0.5rem;
|
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
|
font-size: 0.75rem;
|
|
|
|
|
|
font-family: monospace;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
color: var(--vp-c-brand);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.match-fail {
|
2026-02-06 03:34:50 +08:00
|
|
|
|
text-align: center;
|
2026-02-14 20:23:34 +08:00
|
|
|
|
padding: 0.75rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
color: var(--vp-c-text-3);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.fail-icon {
|
|
|
|
|
|
font-size: 1.5rem;
|
|
|
|
|
|
margin-bottom: 0.5rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.routes-list {
|
2026-02-06 03:34:50 +08:00
|
|
|
|
background: var(--vp-c-bg);
|
2026-02-14 20:23:34 +08:00
|
|
|
|
border-radius: 6px;
|
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);
|
2026-02-13 22:10:03 +08:00
|
|
|
|
margin-bottom: 1rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.routes-grid {
|
2026-02-06 03:34:50 +08:00
|
|
|
|
display: grid;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
|
|
|
|
|
|
gap: 0.5rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.route-item {
|
2026-02-06 03:34:50 +08:00
|
|
|
|
background: var(--vp-c-bg-soft);
|
2026-02-13 22:10:03 +08:00
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
2026-02-06 03:34:50 +08:00
|
|
|
|
border-radius: 6px;
|
2026-02-13 22:10:03 +08:00
|
|
|
|
padding: 0.5rem 0.75rem;
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
gap: 0.25rem;
|
2026-02-06 03:34:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.route-item.matched {
|
|
|
|
|
|
border-color: var(--vp-c-brand);
|
|
|
|
|
|
background: rgba(66, 184, 131, 0.1);
|
2026-02-06 03:34:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.route-path {
|
|
|
|
|
|
font-size: 0.75rem;
|
|
|
|
|
|
font-family: monospace;
|
|
|
|
|
|
color: var(--vp-c-text-1);
|
2026-02-06 03:34:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
.route-name {
|
|
|
|
|
|
font-size: 0.7rem;
|
|
|
|
|
|
color: var(--vp-c-text-3);
|
|
|
|
|
|
}
|
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;
|
|
|
|
|
|
border-radius: 6px;
|
|
|
|
|
|
font-size: 0.85rem;
|
|
|
|
|
|
color: var(--vp-c-text-2);
|
|
|
|
|
|
}
|
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
|
|
|
|
|
2026-02-13 22:10:03 +08:00
|
|
|
|
@media (max-width: 768px) {
|
|
|
|
|
|
.demo-content {
|
2026-02-06 03:34:50 +08:00
|
|
|
|
grid-template-columns: 1fr;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
</style>
|