Kurs Vue.js · Moduł 10: Vue Router
Zagnieżdżone Trasy
W tej lekcji5
Baza NOVA LAB ma moduły, moduły mają systemy, systemy mają komponenty - to hierarchia. Podobnie możemy zagnieżdżać trasy, tworząc złożone layouty z podstronami.
Konfiguracja Nested Routes
Kluczem jest właściwość children w definicji route:
const routes = [
{
path: '/mission-control',
name: 'mission-control',
component: MissionControlLayout,
children: [
{
path: '', // domyślny widok dla /mission-control
name: 'mission-overview',
component: MissionOverview
},
{
path: 'systems', // /mission-control/systems
name: 'mission-systems',
component: MissionSystems
},
{
path: 'crew', // /mission-control/crew
name: 'mission-crew',
component: MissionCrew
}
]
}
]Ważne: ścieżki children BEZ slasha na początku są relatywne do rodzica. Więc 'systems' staje się '/mission-control/systems'.
Parent Component z RouterView
Komponent rodzica MUSI zawierać <RouterView> - to tam renderują się children:
<!-- MissionControlLayout.vue -->
<template>
<div class="layout">
<aside class="sidebar">
<h2>Centrum Kontroli</h2>
<nav>
<RouterLink :to="{ name: 'mission-overview' }">
Przegląd
</RouterLink>
<RouterLink :to="{ name: 'mission-systems' }">
Systemy
</RouterLink>
<RouterLink :to="{ name: 'mission-crew' }">
Załoga
</RouterLink>
</nav>
</aside>
<main class="content">
<!-- Nested routes renderują się tutaj -->
<RouterView />
</main>
</div>
</template>Domyślny Child Route
Pusty path: '' definiuje domyślny widok wyswietlany gdy uzytkownik wchodzi na ścieżkę rodzica:
children: [
{
path: '', // /mission-control
component: MissionOverview
},
{
path: 'systems', // /mission-control/systems
component: MissionSystems
}
]Redirect
Można tez uzyc przekierowania zamiast domyślnego widoku:
children: [
{
path: '',
redirect: '/mission-control/dashboard'
},
{
path: 'dashboard',
component: Dashboard
}
]Named Views
Jeden route może renderowac wiele komponentow w różnych <RouterView>:
const routes = [
{
path: '/mission-control',
components: {
default: MissionMain,
sidebar: MissionSidebar,
footer: MissionFooter
}
}
]<template>
<RouterView name="sidebar" />
<RouterView /> <!-- default -->
<RouterView name="footer" />
</template>Kod do tej lekcji: App.vue
<script setup>
import { ref } from 'vue'
// Symulacja navigation guards
const isAuthenticated = ref(false)
const currentRoute = ref('home')
const guardLog = ref([])
const pageTitle = ref('NOVA LAB - Home')
const routes = [
{ name: 'home', label: 'Home', meta: {} },
{ name: 'login', label: 'Login', meta: { requiresGuest: true } },
{ name: 'dashboard', label: 'Dashboard', meta: { requiresAuth: true } },
{ name: 'admin', label: 'Admin', meta: { requiresAuth: true, requiresAdmin: true } }
]
function navigate(routeName) {
const route = routes.find(r => r.name === routeName)
if (!route) return
guardLog.value = []
// Symulacja beforeEach
guardLog.value.push('beforeEach: ' + currentRoute.value + ' -> ' + routeName)
// Sprawdz requiresAuth
if (route.meta.requiresAuth && !isAuthenticated.value) {
guardLog.value.push('BLOCKED: requiresAuth - redirect to login')
currentRoute.value = 'login'
pageTitle.value = 'NOVA LAB - Login'
return
}
// Sprawdz requiresGuest
if (route.meta.requiresGuest && isAuthenticated.value) {
guardLog.value.push('BLOCKED: requiresGuest - redirect to home')
currentRoute.value = 'home'
pageTitle.value = 'NOVA LAB - Home'
return
}
// Nawigacja dozwolona
guardLog.value.push('ALLOWED: navigation ok')
currentRoute.value = routeName
pageTitle.value = 'NOVA LAB - ' + route.label
// Symulacja afterEach
guardLog.value.push('afterEach: title = ' + pageTitle.value)
}
function toggleAuth() {
isAuthenticated.value = !isAuthenticated.value
guardLog.value.push('Auth: ' + (isAuthenticated.value ? 'zalogowany' : 'wylogowany'))
}
</script>
<template>
<div class="nova-lab">
<h1>NOVA LAB - Navigation Guards</h1>
<div class="auth-bar">
<span>Status: {{ isAuthenticated ? 'Zalogowany' : 'Niezalogowany' }}</span>
<button @click="toggleAuth" class="auth-btn">
{{ isAuthenticated ? 'Wyloguj' : 'Zaloguj' }}
</button>
</div>
<nav class="main-nav">
<button
v-for="route in routes"
:key="route.name"
@click="navigate(route.name)"
:class="{ active: currentRoute === route.name }"
class="nav-btn"
>
{{ route.label }}
<span v-if="route.meta.requiresAuth" class="badge lock">LOCK</span>
<span v-if="route.meta.requiresGuest" class="badge guest">GUEST</span>
</button>
</nav>
<div class="guard-log">
<h3>Guard Log:</h3>
<div v-for="(log, i) in guardLog" :key="i" class="log-entry"
:class="{
blocked: log.includes('BLOCKED'),
allowed: log.includes('ALLOWED')
}"
>
{{ log }}
</div>
</div>
<div class="page-content">
<h2>{{ pageTitle }}</h2>
<pre>
// Konfiguracja guards:
router.beforeEach((to, from) => {
if (to.meta.requiresAuth && !isAuthenticated) {
return { name: 'login' }
}
})
router.afterEach((to) => {
document.title = to.meta.title || 'NOVA LAB'
})</pre>
</div>
</div>
</template>
<style scoped>
.nova-lab {
background: #0a0e27;
color: #00ff88;
min-height: 100vh;
padding: 20px;
font-family: monospace;
}
h1 { color: #00ffff; margin-bottom: 10px; }
h2 { color: #00b4d8; }
h3 { color: #00ffff; margin-bottom: 5px; }
.auth-bar {
display: flex;
align-items: center;
gap: 10px;
padding: 8px 12px;
background: #1a1e37;
margin-bottom: 10px;
color: #c0c0c0;
}
.auth-btn {
background: rgba(0, 255, 136, 0.2);
border: 1px solid #00ff88;
color: #00ff88;
padding: 4px 10px;
cursor: pointer;
font-family: monospace;
}
.main-nav { display: flex; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.nav-btn {
background: rgba(0, 180, 216, 0.1);
border: 1px solid #00b4d8;
color: #00ff88;
padding: 6px 12px;
cursor: pointer;
font-family: monospace;
position: relative;
}
.nav-btn.active {
background: #00b4d8;
color: #0a0e27;
}
.badge {
font-size: 9px;
padding: 1px 4px;
margin-left: 4px;
}
.badge.lock { color: #ff4444; }
.badge.guest { color: #ffaa00; }
.guard-log {
background: #1a1e37;
padding: 10px;
margin-bottom: 10px;
max-height: 120px;
overflow-y: auto;
}
.log-entry {
font-size: 12px;
color: #888;
padding: 2px 0;
}
.log-entry.blocked { color: #ff4444; }
.log-entry.allowed { color: #00ff88; }
.page-content {
background: rgba(0, 180, 216, 0.05);
border: 1px solid #1a1e37;
padding: 15px;
}
pre {
background: #1a1e37;
padding: 10px;
color: #00ff88;
font-size: 11px;
margin-top: 10px;
}
</style>Hasła z dokumentacji w tej lekcji
Widzisz błąd w tej lekcji?
Sprawdź się
Odpowiedz na pytania z tej lekcji. Wybierz odpowiedź, a od razu zobaczysz, czy jest poprawna.
1. Jak zdefiniować zagnieżdżone (nested) routes w Vue Router?
2. Gdzie renderują się komponenty zagnieżdżonych routes?
To 2 z 4 pytań do tej lekcji. Pozostałe rozwiążesz w grze.
Zadania praktyczne w grze
- Edytor kodu
Skonfiguruj parent route z children array i layout z <RouterView>.
- Układanie w pionie
Uporządkuj elementy od zewnętrznego do wewnętrznego:
- Klikanie w kolejności
Ułóż kod definicji nested route:
- Edytor kodu
Dodaj domyślny child route z pustym path i komponent overview.
Zadania po tej lekcji
- PytanieWłaściwość children
- PytanieRenderowanie nested routes
- Zadanie z kodemPraktyka: Nested routes z layout
- Ułóż w kolejnościHierarchia nested routes
- PytanieRedirect w routes
- Kliknij w kolejnościSkładnia nested route z children
- Zadanie z kodemPraktyka: Default child route
- PytanieSpecyficzność tras w Vue Router