📖 Scenario: You are building a simple Angular app for a bookstore. The app has a main page showing book categories. When a user clicks a category, they see a list of books in that category. When they click a book, they see details about that book. You will use child routes and nested routing to organize these views.
🎯 Goal: Create Angular routes with child routes to show categories, books in a category, and book details using nested routing.
📋 What You'll Learn
Create a main route for categories
Add child routes for books inside categories
Add nested child routes for book details inside books
Use Angular's RouterModule.forRoot() with the correct route configuration
Use standalone components for each route
💡 Why This Matters
🌍 Real World
Nested routing is common in apps with hierarchical data, like categories and items, user profiles with subpages, or admin dashboards with sections.
💼 Career
Understanding child routes and nested routing is essential for Angular developers building scalable and maintainable web applications.
Progress0 / 4 steps
1
Set up the main routes array
Create a constant called routes that is an array with one route object. The route should have path set to 'categories' and component set to CategoriesComponent.
Angular
Hint
Remember to import Routes from @angular/router and CategoriesComponent.
2
Add child routes for books inside categories
Add a children property to the 'categories' route. It should be an array with one child route object. This child route should have path set to 'books' and component set to BooksComponent.
Angular
Hint
Use the children property to nest routes inside the 'categories' route.
3
Add nested child routes for book details inside books
Inside the children array of the 'categories' route, find the 'books' route object. Add a children property to it. This should be an array with one route object. This nested route should have path set to ':id' and component set to BookDetailsComponent.
Angular
Hint
Use children again inside the 'books' route to nest the book details route.
4
Configure RouterModule with the routes
Import RouterModule from @angular/router and export a constant called AppRoutingModule using RouterModule.forRoot(routes).
Angular
Hint
Use @NgModule to create AppRoutingModule that imports and exports RouterModule.
Practice
(1/5)
1. What is the main purpose of child routes in Angular routing?
easy
A. To replace the main router outlet completely
B. To disable navigation between pages
C. To load components without URLs
D. To nest pages inside other pages for better organization
Solution
Step 1: Understand child routes concept
Child routes allow nesting routes inside a parent route to organize pages hierarchically.
Step 2: Compare options with concept
Only To nest pages inside other pages for better organization correctly describes this purpose; others are incorrect or unrelated.
Final Answer:
To nest pages inside other pages for better organization -> Option D
Quick Check:
Child routes = Nested pages [OK]
Hint: Child routes organize nested pages inside parents [OK]
Common Mistakes:
Thinking child routes replace main router outlet
Believing child routes work without URLs
Confusing child routes with disabling navigation
2. Which syntax correctly defines a child route in Angular's RouterModule?
easy
A. { path: 'parent', children: ChildComponent }
B. { path: 'parent/child', component: ChildComponent }
The 'children' property must be an array of route objects, not a single object.
Step 2: Verify given config
Here, 'children' is incorrectly assigned an object instead of an array.
Final Answer:
'children' should be an array, not an object -> Option A
Quick Check:
'children' = array of routes [OK]
Hint: 'children' must always be an array, not a single object [OK]
Common Mistakes:
Using object instead of array for children
Confusing 'path' usage inside children
Assuming missing component causes this error
5. You want to create a nested route where /settings loads SettingsComponent and /settings/profile loads ProfileComponent inside it. Which setup is correct?
hard
A. Define { path: 'settings', component: SettingsComponent, children: [{ path: 'profile', component: ProfileComponent }] } and add <router-outlet> in SettingsComponent template
B. Define { path: 'settings/profile', component: ProfileComponent } only, no children needed