Vue3 Fundamentals ยท Vue Router

Nested routes and layouts

Nested routes and layouts

A settings area has several pages that share one sidebar. Repeating the sidebar in every page is wasteful, so Vue Router lets a route nest its children inside a parent.

const routes = [
  {
    path: '/settings',
    component: SettingsLayout,
    children: [
      { path: 'profile', component: Profile },
      { path: 'billing', component: Billing },
    ],
  },
];

The parent route renders once and stays mounted while its children change. Its component holds a second <RouterView />, and that is where the matched child appears:

<!-- SettingsLayout.vue -->
<template>
  <aside>Settings</aside>
  <RouterView />
</template>

A child path without a leading slash is relative to its parent, so the children above match /settings/profile and /settings/billing. The parent's path is absolute; the child's is not.

Nested routes are how you build layouts: the parent owns the chrome that should persist, and the child owns the content that swaps. You can nest further, and each level renders into the <RouterView /> of the level above it.

You are about to answer a short quiz on nested routes and layouts.

  1. Child routes are declared in the parent route's ___ array.
  2. A nested child route renders into the `<RouterView />` of its parent component.
  3. A child `path` with a leading slash is relative to its parent.
  4. A child path of `profile` under a parent path of `/settings` matches ___.
  5. Nested routes let a parent layout stay mounted while its children change.
  6. You can nest routes more than one level deep.