Nested routes and named views

Intermediate · 7 min

Persistent layouts with children routes and a nested RouterView, default child routes, multiple named outlets, and caching routed views with KeepAlive.

Before thisDynamic routes and paramsKeepAlive and cached components

Shawn Wildermuth · 7 min

Nested routes and named views

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Shawn WildermuthReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A route record's `children` array nests routes: the parent's component renders its own `<RouterView />`, and the matched child renders inside it, so the parent layout stays mounted while children swap.
  • Child paths are relative — `profile` under `/users/:id` matches `/users/:id/profile` — while a child path starting with `/` is treated as a root path.
  • A child with `path: ''` is the default view rendered at the parent's own URL, and it is usually the child, not the parent, that gets the route `name`.
  • Named views put several outlets on one screen: `<RouterView name="sidebar" />` plus a route's `components` (plural) object with `default` and `sidebar` keys.
  • A parent route with no `component` just groups children under a shared path prefix and shared guards, without adding a wrapper component.
  • `<RouterView v-slot="{ Component }">` hands you the matched component so you can wrap it in `<KeepAlive>` or `<Transition>` yourself.

The problem: a layout that shouldn’t reload

Open any account settings page. There’s a side menu — Profile, Billing, Notifications — and a content panel. Click Billing and the URL changes to /settings/billing, the panel changes, and the side menu stays exactly where it was, scroll position and all.

With a flat route table you’d have two bad options. Copy the side menu into every settings page, or build a SettingsLayout wrapper and remember to use it in each one. Both throw the menu away and rebuild it on every click, because each URL maps to a different top-level component.

The URL already describes the structure you want: /settings is the frame and /billing is what sits inside it. Nested routes let the route table say the same thing. The frontend roadmap introduced nested routes as an idea. This lesson covers Vue Router’s version of it.

Children and a nested RouterView

A route record can carry a children array, which is just another list of route records:

const routes = [
  {
    path: '/settings',
    component: SettingsLayout,
    children: [
      { path: '', name: 'settings', component: SettingsOverview },
      { path: 'profile', name: 'settings-profile', component: ProfileSettings },
      { path: 'billing', name: 'settings-billing', component: BillingSettings },
    ],
  },
]

The parent component marks where children go with its own <RouterView />:

<!-- SettingsLayout.vue -->
<script setup>
import { RouterLink, RouterView } from 'vue-router'
</script>

<template>
  <div class="settings">
    <aside>
      <RouterLink :to="{ name: 'settings-profile' }">Profile</RouterLink>
      <RouterLink :to="{ name: 'settings-billing' }">Billing</RouterLink>
    </aside>
    <section>
      <RouterView />
    </section>
  </div>
</template>

Each <RouterView> renders one level of the match. For /settings/billing the router finds the chain SettingsLayout → BillingSettings. The top-level <RouterView> in App.vue renders SettingsLayout, and the <RouterView> inside SettingsLayout renders BillingSettings. Moving to /settings/profile only changes the second level, so SettingsLayout is never unmounted.

Think of a picture frame with a removable insert. Swapping the photo doesn’t mean rehanging the frame.

You can nest as deep as you like — a children record can have its own children and its component its own <RouterView>.

How child paths resolve

Child paths are relative to the parent: billing under /settings matches /settings/billing. Write the segment you’re adding, not the whole path. A child path that starts with / is treated as a root path. It still renders nested inside the parent’s component, but its URL doesn’t include the parent’s prefix. That’s occasionally handy and often a typo.

Params flow down too. Under /users/:id, a child at posts matches /users/42/posts, and the child sees route.params.id. Combine that with the reused-component behaviour from the last lesson: going from /users/1/posts to /users/2/posts reuses both levels.

String to values in <RouterLink> resolve like URLs in HTML, not like route records. to="billing" without a leading slash is relative to the current URL, which surprises people. Named routes sidestep the problem entirely.

The default child, and where the name goes

With only profile and billing as children, visiting /settings itself renders SettingsLayout with an empty panel. The path: '' child in the example fills it: an empty path matches the parent’s own URL, so SettingsOverview shows up at plain /settings.

Notice which record carries name: 'settings': the empty-path child, not the parent. A by-name link targets one exact record. Naming the child means { name: 'settings' } always renders the layout with the overview inside it. Naming only the parent would let a by-name navigation render the layout with nothing in the outlet. A page reload of the same URL would then match the empty-path child and show it. One URL, two different screens.

Parents without a component

Sometimes children share a path prefix or a guard but no visual frame. Since Vue Router 4.1 you can leave component off the parent:

{
  path: '/admin',
  meta: { requiresAdmin: true },
  children: [
    { path: '', component: AdminDashboard },
    { path: 'users', component: AdminUsers },
    { path: 'users/:id', component: AdminUserDetail },
  ],
}

The parent contributes its path and its meta (and any beforeEnter guard), and the top-level <RouterView> renders the child directly. That’s a clean way to put a whole section behind one check, which navigation guards builds on.

Named views: several outlets at once

Nesting answers “a frame with something inside it.” Named views answer a different question: “one URL that fills several regions side by side.” Picture a dashboard where the main panel and a contextual sidebar both change per route.

Give each extra outlet a name. The unnamed one is called default:

<!-- App.vue -->
<template>
  <AppHeader />
  <div class="shell">
    <RouterView name="sidebar" />
    <RouterView />
  </div>
</template>

Then use components, plural, to fill them per route:

const routes = [
  {
    path: '/',
    components: { default: HomeFeed, sidebar: TrendingSidebar },
  },
  {
    path: '/projects/:id',
    components: { default: ProjectBoard, sidebar: ProjectMembers },
    props: { default: true, sidebar: true }, // props config is per view
  },
  {
    path: '/settings',
    component: SettingsLayout, // no sidebar component → that outlet renders nothing
  },
]

A route that doesn’t supply a component for some named outlet leaves it empty. Named views also combine with nesting: a child record can use components to fill several named <RouterView>s inside its parent’s template, the same way. Compared with React Router, which has no named outlets, this is a Vue Router feature without a direct equivalent. In React the usual workaround is rendering the sidebar from the layout based on the current match.

Use named views sparingly. If the “sidebar” is the same component everywhere with different data, a plain component that reads the route is simpler than mapping it on every record.

RouterView’s slot: KeepAlive and transitions

By default, moving from one child to another unmounts the old child. Its local state goes with it, so a half-scrolled list or a half-typed draft is lost. The <KeepAlive> lesson showed how to cache component instances. With the router you reach the matched component through <RouterView>’s scoped slot:

<!-- inside SettingsLayout.vue -->
<RouterView v-slot="{ Component }">
  <Transition name="fade" mode="out-in">
    <KeepAlive :include="['ProfileSettings']">
      <component :is="Component" />
    </KeepAlive>
  </Transition>
</RouterView>

The slot gives you Component, the matched component for this level, plus the route it was matched from if you need it. Rendering it yourself with <component :is> lets you wrap it in <Transition> and <KeepAlive> in that order. Wrapping <RouterView> directly in <KeepAlive> doesn’t work in Vue 3 — the slot is the supported way. :include matches component names, which <script setup> components infer from their filename.

What the video covers

Shawn Wildermuth’s seven-minute short covers the core of the first half. He starts from a working flat router, explains how the match drives <RouterView>, adds a children array, drops a nested <RouterView> into the parent page, links to the children with <RouterLink>, and adds a second child to show the parent staying put. It’s tight and accurate, Vue 3 and Vue Router 4 throughout. It doesn’t reach the default empty-path child, parents without components, named views, or the v-slot pattern for <KeepAlive> — the sections above carry those. For a demo of named views specifically, the first “go deeper” link builds a three-outlet layout with components and per-view props.

Check yourself

  1. 01A `/settings` route has `children` for `profile` and `billing`, but visiting `/settings/profile` shows only the settings header with nothing underneath. What is the most likely mistake?

    The parent component, `Settings`, has no `<RouterView />` in its template. Child routes render into the nearest `<RouterView>` inside their parent's component, so without one the matched child has nowhere to go.

  2. 02What is the difference between `component` and `components` on a route record?

    `component` is a single component rendered into the unnamed (`default`) `<RouterView>`. `components` is an object mapping view names to components — `{ default: Main, sidebar: Sidebar }` — used with named views so one route can fill several `<RouterView name="...">` outlets at once.

  3. 03Under `/users/:id`, why is it usually the empty-path child (`path: ''`) that gets `name: 'user'`, rather than the parent record?

    A by-name link or `push` targets that exact record. If the name is on the empty-path child, `{ name: 'user', params: { id } }` lands on `/users/:id` with the default child rendered inside the layout. If only the parent were named, a by-name `push` would render the layout with an empty nested outlet, while reloading the same URL would match the empty-path child and show it — two different screens for one URL.

  4. 04Why does Vue Router 4+ require `<RouterView v-slot="{ Component }">` to use `<KeepAlive>`, instead of wrapping `<RouterView>` directly?

    `<KeepAlive>` and `<Transition>` act on their direct child component, and `<RouterView>` itself is not the component you want cached or animated. The slot exposes the actual matched component, so you render it with `<component :is="Component" />` inside `<KeepAlive>` and the right instance gets cached.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark