Nested routes let you show pages inside other pages. This helps organize your app and keep related views together.
Nested routes and child views in Vue
const routes = [ { path: '/parent', component: ParentComponent, children: [ { path: 'child', component: ChildComponent } ] } ];
The children array holds nested routes inside a parent route.
Child paths do not start with a slash; they are relative to the parent path.
const routes = [ { path: '/dashboard', component: Dashboard, children: [ { path: 'stats', component: Stats }, { path: 'settings', component: Settings } ] } ];
'' as a child path means it shows by default when visiting the parent route.const routes = [ { path: '/profile', component: Profile, children: [ { path: '', component: ProfileOverview }, { path: 'posts', component: ProfilePosts } ] } ];
This Vue app shows a parent view with a nested child view. When you visit /parent/child, you see the child content inside the parent layout.
<template> <router-view /> </template> <script setup> import { createRouter, createWebHistory } from 'vue-router'; import { createApp } from 'vue'; const Parent = { template: `<div> <h2>Parent View</h2> <router-view /> </div>` }; const Child = { template: `<div><p>This is the child view inside the parent.</p></div>` }; const routes = [ { path: '/parent', component: Parent, children: [ { path: 'child', component: Child } ] } ]; const router = createRouter({ history: createWebHistory(), routes }); const app = createApp({}); app.use(router); app.mount('#app'); </script>
Always include a <router-view /> in the parent component to show child views.
Child routes build on the parent path automatically, so no leading slash in child paths.
Use nested routes to keep your app organized and improve user experience with clear layouts.
Nested routes let you show smaller views inside bigger views.
Child routes go inside a children array in the parent route.
Use <router-view /> in parent components to display child views.
Practice
Solution
Step 1: Understand nested routes concept
Nested routes allow smaller views (child views) to be shown inside bigger views (parent views).Step 2: Identify how child views are displayed
Child views appear inside the parent component where<router-view />is placed.Final Answer:
To display child views inside a parent view using <router-view /> -> Option BQuick Check:
Nested routes = child views inside parent with <router-view /> [OK]
- Thinking nested routes load separate apps
- Believing nested routes don't need
- Confusing nested routes with route guards
Solution
Step 1: Recall Vue Router child route syntax
Child routes go inside achildrenarray property of the parent route object.Step 2: Check each option's structure
{ path: '/parent', component: Parent, children: [{ path: 'child', component: Child }] } correctly useschildren: [{ path: 'child', component: Child }]. Options C and D use wrong keys or types. { path: '/parent/child', component: Child } defines a separate route, not nested.Final Answer:
{ path: '/parent', component: Parent, children: [{ path: 'child', component: Child }] } -> Option AQuick Check:
Child routes = inside children array [OK]
- Using 'child' instead of 'children' key
- Defining child routes outside parent route
- Using object instead of array for children
/dashboard/settings?
const routes = [
{ path: '/dashboard', component: Dashboard, children: [
{ path: 'settings', component: Settings }
]}
];
// Dashboard.vue template:
// Dashboard
// Settings.vue template:
// Settings Page
Solution
Step 1: Understand nested route rendering
When visiting '/dashboard/settings', Vue renders Dashboard component first, then inside its<router-view />renders Settings component.Step 2: Combine rendered HTML from both components
Dashboard shows<h1>Dashboard</h1>and Settings shows<h2>Settings Page</h2>. Both appear together.Final Answer:
<h1>Dashboard</h1><h2>Settings Page</h2> -> Option CQuick Check:
Nested route renders parent + child views [OK]
- Expecting only child component to render
- Forgetting in parent
- Confusing full path with child path
const routes = [
{ path: '/profile', component: Profile, children: [
{ path: 'edit', component: EditProfile }
]}
];
// Profile.vue template:
// Profile Page
Solution
Step 1: Check how child routes display
Child routes render inside the parent's<router-view />. Without it, child views won't show.Step 2: Inspect Profile.vue template
Profile.vue only has<h1>Profile Page</h1>and no<router-view />, so child routes can't render.Final Answer:
Profile.vue is missing <router-view /> to display child routes -> Option AQuick Check:
Missing <router-view /> = no child views [OK]
- Adding '/' at start of child path incorrectly
- Placing children outside parent route object
- Assuming child component imports affect rendering
/shop shows a Shop component and /shop/products shows a Products component inside Shop. Which setup correctly achieves this and ensures accessibility and responsiveness?
A) Define routes with children array, use <router-view /> in Shop, and add ARIA roles and responsive CSS.
B) Define separate routes for /shop and /shop/products without nesting, and use CSS media queries only.
C) Use nested routes but omit <router-view /> in Shop and rely on v-if to show Products.
D) Use nested routes with children but place <router-view /> outside Shop component.Solution
Step 1: Setup nested routes properly
Use a children array in the /shop route to define /shop/products as a child route.Step 2: Ensure Shop component includes <router-view />
This allows Products component to render inside Shop when navigating to /shop/products.Step 3: Add accessibility and responsiveness
Use ARIA roles for landmarks and semantic HTML, plus responsive CSS (like Flexbox or Grid) to adapt layout on different screens.Final Answer:
Define routes with children array, use <router-view /> in Shop, add ARIA roles and responsive CSS -> Option DQuick Check:
Nested routes + router-view + accessibility + responsive CSS = Define routes with children array, use in Shop, add ARIA roles and responsive CSS [OK]
- Omitting router-view in parent component
- Not adding ARIA roles for accessibility
- Ignoring responsive design in CSS
- Placing router-view outside parent component
Ready to prove it?
Pass the test and this pattern moves unknown
