📖 Scenario: You are building a simple website with a main page and a user profile section. The profile section has two parts: Overview and Settings. You want to show these parts inside the profile page without leaving it, using nested routes and child views.
🎯 Goal: Create a Vue app with nested routes where the /profile route shows a profile layout and inside it, the /profile/overview and /profile/settings routes show their respective child views.
📋 What You'll Learn
Create a Vue router with nested routes for /profile and its children overview and settings
Create a ProfileLayout component that includes a <router-view> for child views
Create simple Overview and Settings components
Use router-link to navigate between overview and settings inside the profile page
💡 Why This Matters
🌍 Real World
Nested routes let you build complex pages with sections that change without leaving the main page, like user dashboards or settings panels.
💼 Career
Understanding nested routes is essential for building scalable Vue apps with clear navigation and reusable layouts.
Progress0 / 4 steps
1
Set up the main Vue router with a /profile route
Create a Vue router with a route for /profile that uses a component called ProfileLayout. Import createRouter and createWebHistory from 'vue-router'.
Vue
Hint
Remember to import ProfileLayout and set it as the component for the /profile route.
2
Add child routes overview and settings under /profile
Inside the /profile route, add a children array with two routes: one for overview using component Overview, and one for settings using component Settings. Import both components.
Vue
Hint
Child routes do not start with a slash. Use path: 'overview' and path: 'settings'.
3
Create ProfileLayout component with a <router-view>
Create a Vue component called ProfileLayout.vue that has a heading <h2>User Profile</h2>, two router-link elements to /profile/overview and /profile/settings, and a <router-view> to show child views.
Vue
Hint
Use <router-link> for navigation and <router-view> to display child routes inside the layout.
4
Create simple Overview and Settings components
Create two Vue components: Overview.vue with a heading <h3>Profile Overview</h3> and Settings.vue with a heading <h3>Profile Settings</h3>. These will be the child views shown inside ProfileLayout.
Vue
Hint
Each component should have a heading and some text inside a <div>.
Practice
(1/5)
1. What is the main purpose of nested routes in Vue Router?
easy
A. To load multiple Vue apps on the same page
B. To display child views inside a parent view using <router-view />
C. To create routes without components
D. To disable navigation guards
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 B
C. Children array should be outside the parent route object
D. EditProfile component must be imported inside Profile.vue
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 A
Quick Check:
Missing <router-view /> = no child views [OK]
Hint: Parent needs to show child routes [OK]
Common Mistakes:
Adding '/' at start of child path incorrectly
Placing children outside parent route object
Assuming child component imports affect rendering
5. You want to create a nested route where /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.
hard
A. Use nested routes but place outside Shop component
B. Define separate routes without nesting and use only CSS media queries
C. Use nested routes but omit in Shop and use v-if for Products
D. Define routes with children array, use in Shop, add ARIA roles and responsive CSS
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 D
Quick Check:
Nested routes + router-view + accessibility + responsive CSS = Define routes with children array, use in Shop, add ARIA roles and responsive CSS [OK]
Hint: Nested routes need router-view in parent plus ARIA and responsive CSS [OK]
Common Mistakes:
Omitting router-view in parent component
Not adding ARIA roles for accessibility
Ignoring responsive design in CSS
Placing router-view outside parent component
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.