Vue.js course ยท Module 10: Vue Router

Nested Routes

2 min read ยท
In this lesson5

The NOVA LAB base has modules, modules have systems, systems have components - it's a hierarchy. Similarly, we can nest routes, creating complex layouts with subpages.

Nested Routes Configuration

The key is the children property in the route definition:

const routes = [
  {
    path: '/mission-control',
    name: 'mission-control',
    component: MissionControlLayout,
    children: [
      {
        path: '',  // default view for /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
      }
    ]
  }
]

Important: children paths WITHOUT a leading slash are relative to the parent. So 'systems' becomes '/mission-control/systems'.

Parent Component with RouterView

The parent component MUST contain <RouterView> - that's where children render:

<!-- MissionControlLayout.vue -->
<template>
  <div class="layout">
    <aside class="sidebar">
      <h2>Control Center</h2>
      <nav>
        <RouterLink :to="{ name: 'mission-overview' }">
          Overview
        </RouterLink>
        <RouterLink :to="{ name: 'mission-systems' }">
          Systems
        </RouterLink>
        <RouterLink :to="{ name: 'mission-crew' }">
          Crew
        </RouterLink>
      </nav>
    </aside>

    <main class="content">
      <!-- Nested routes render here -->
      <RouterView />
    </main>
  </div>
</template>

Default Child Route

An empty path: '' defines the default view displayed when the user navigates to the parent path:

children: [
  {
    path: '',  // /mission-control
    component: MissionOverview
  },
  {
    path: 'systems',  // /mission-control/systems
    component: MissionSystems
  }
]

Redirect

You can also use a redirect instead of a default view:

children: [
  {
    path: '',
    redirect: '/mission-control/dashboard'
  },
  {
    path: 'dashboard',
    component: Dashboard
  }
]

Named Views

A single route can render multiple components in different <RouterView> slots:

const routes = [
  {
    path: '/mission-control',
    components: {
      default: MissionMain,
      sidebar: MissionSidebar,
      footer: MissionFooter
    }
  }
]
<template>
  <RouterView name="sidebar" />
  <RouterView /> <!-- default -->
  <RouterView name="footer" />
</template>
Code for this lesson: App.vue
<script setup>
import { ref } from 'vue'

// Simulation of 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 = []

  // Simulation of beforeEach
  guardLog.value.push('beforeEach: ' + currentRoute.value + ' -> ' + routeName)

  // Check requiresAuth
  if (route.meta.requiresAuth && !isAuthenticated.value) {
    guardLog.value.push('BLOCKED: requiresAuth - redirect to login')
    currentRoute.value = 'login'
    pageTitle.value = 'NOVA LAB - Login'
    return
  }

  // Check requiresGuest
  if (route.meta.requiresGuest && isAuthenticated.value) {
    guardLog.value.push('BLOCKED: requiresGuest - redirect to home')
    currentRoute.value = 'home'
    pageTitle.value = 'NOVA LAB - Home'
    return
  }

  // Navigation allowed
  guardLog.value.push('ALLOWED: navigation ok')
  currentRoute.value = routeName
  pageTitle.value = 'NOVA LAB - ' + route.label

  // Simulation of afterEach
  guardLog.value.push('afterEach: title = ' + pageTitle.value)
}

function toggleAuth() {
  isAuthenticated.value = !isAuthenticated.value
  guardLog.value.push('Auth: ' + (isAuthenticated.value ? 'logged in' : 'logged out'))
}
</script>

<template>
  <div class="nova-lab">
    <h1>NOVA LAB - Navigation Guards</h1>

    <div class="auth-bar">
      <span>Status: {{ isAuthenticated ? 'Logged in' : 'Logged out' }}</span>
      <button @click="toggleAuth" class="auth-btn">
        {{ isAuthenticated ? 'Log out' : 'Log in' }}
      </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>
// Guards configuration:
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>

Docs entries used in this lesson

Spotted a mistake in this lesson?

Check yourself

Answer the questions from this lesson. Pick an answer to see right away whether it is correct.

  1. 1. How do you define nested routes in Vue Router?

  2. 2. Where are nested route components rendered?

These are 2 of 4 questions for this lesson. Solve the rest in the game.

Hands-on tasks in the game

  • Code editor

    Configure a parent route with a children array and a layout with <RouterView>.

  • Vertical ordering

    Arrange the elements from outermost to innermost:

  • Click in order

    Arrange the nested route definition code:

  • Code editor

    Add a default child route with an empty path and an overview component.

  1. QuestionThe children Property
  2. QuestionRendering Nested Routes
  3. Coding taskPractice: Nested Routes with Layout
  4. Put in orderNested Routes Hierarchy
  5. QuestionRedirect in Routes
  6. Click in orderNested Route Definition Syntax
  7. Coding taskPractice: Default Child Route
  8. QuestionRoute Specificity in Vue Router

Useful articles