Angular Child Routes
Learn Angular Child Routes in detail with nested routing, RouterOutlet, standalone routing, lazy loading, route guards, architecture diagrams, enterprise examples, best practices, and interview questions.
Child Routes allow Angular applications to organize navigation hierarchically by placing routes inside other routes.
Instead of having all routes at the root level, Angular enables you to create nested routes, making applications more modular and easier to maintain.
Child Routes are commonly used in:
- Admin Dashboards
- Banking Applications
- CRM Systems
- E-Commerce Websites
- Learning Management Systems (LMS)
- ERP Applications
They are one of the most frequently asked Angular Routing interview topics.
Table of Contents
- What are Child Routes?
- Why Child Routes?
- Parent vs Child Routes
- Configuring Child Routes
- Nested RouterOutlet
- Navigation
- Route Parameters
- Lazy Loaded Child Routes
- Route Guards with Child Routes
- Enterprise Banking Example
- Best Practices
- Common Mistakes
- Top 10 Interview Questions
- Summary
What are Child Routes?
A Child Route is a route that exists under a parent route.
Example URL hierarchy
/dashboard
/dashboard/accounts
/dashboard/payments
/dashboard/profile
/dashboard/settings
Here,
dashboardis the parent route.accounts,payments,profile, andsettingsare child routes.
Child Route Architecture
flowchart TD
Application
Application --> Dashboard
Dashboard --> Accounts
Dashboard --> Payments
Dashboard --> Profile
Dashboard --> Settings
Why Use Child Routes?
Without Child Routes
/
↓
dashboard
↓
accounts
↓
payments
↓
profile
↓
settings
Problems
- Flat routing
- Difficult organization
- Hard to scale
- Duplicate layouts
With Child Routes
Dashboard
↓
Accounts
↓
Payments
↓
Profile
↓
Settings
Benefits
- Hierarchical navigation
- Shared layout
- Better scalability
- Cleaner routing
- Easier maintenance
Parent vs Child Routes
| Parent Route | Child Route |
|---|---|
| Top-level route | Nested under parent |
| Usually contains layout | Displays feature content |
Contains <router-outlet> |
Renders inside parent |
| Can own shared UI | Focuses on feature |
Configuring Child Routes
import { Routes } from '@angular/router';
export const routes: Routes = [
{
path: 'dashboard',
component: DashboardComponent,
children: [
{
path: 'accounts',
component: AccountsComponent
},
{
path: 'payments',
component: PaymentsComponent
},
{
path: 'profile',
component: ProfileComponent
}
]
}
];
Route Configuration Architecture
flowchart TD
Routes
Routes --> DashboardRoute
DashboardRoute --> AccountsRoute
DashboardRoute --> PaymentsRoute
DashboardRoute --> ProfileRoute
Parent Component
The parent component contains the shared layout.
@Component({
selector: 'app-dashboard',
standalone: true,
templateUrl: './dashboard.component.html'
})
export class DashboardComponent {
}
Parent Template
<h2>Dashboard</h2>
<nav>
<a routerLink="accounts">
Accounts
</a>
<a routerLink="payments">
Payments
</a>
<a routerLink="profile">
Profile
</a>
</nav>
<hr>
<router-outlet></router-outlet>
The nested router-outlet displays the selected child component.
Nested RouterOutlet
flowchart LR
DashboardComponent
DashboardComponent --> RouterOutlet
RouterOutlet --> AccountsComponent
RouterOutlet --> PaymentsComponent
RouterOutlet --> ProfileComponent
Navigating to Child Routes
Template navigation
<a routerLink="accounts">
Accounts
</a>
<a routerLink="payments">
Payments
</a>
Programmatic navigation
constructor(
private router: Router
) {}
goToAccounts() {
this.router.navigate([
'/dashboard',
'accounts'
]);
}
Navigation Flow
sequenceDiagram
participant User
participant Dashboard
participant Router
participant Accounts
User->>Dashboard: Click Accounts
Dashboard->>Router: routerLink
Router->>Accounts: Load Component
Accounts-->>User: Display View
Child Route Parameters
Configuration
{
path: 'employees/:id',
component: EmployeeDetailsComponent
}
Navigation
/dashboard/employees/101
Reading the parameter
constructor(
private route: ActivatedRoute
) {}
employeeId =
this.route.snapshot.paramMap.get('id');
Parameter Flow
flowchart LR
URL
URL --> Router
Router --> ChildRoute
ChildRoute --> Component
Default Child Route
Display a default child when users navigate to the parent.
{
path: 'dashboard',
component: DashboardComponent,
children: [
{
path: '',
redirectTo: 'accounts',
pathMatch: 'full'
},
{
path: 'accounts',
component: AccountsComponent
}
]
}
Visiting
/dashboard
automatically redirects to
/dashboard/accounts
Child Route Redirection
flowchart TD
Dashboard
Dashboard --> DefaultRoute
DefaultRoute --> Accounts
Lazy Loaded Child Routes
Large feature areas can lazy load child routes.
{
path: 'admin',
loadChildren: () =>
import('./admin/admin.routes')
.then(r => r.ADMIN_ROUTES)
}
Admin feature
export const ADMIN_ROUTES: Routes = [
{
path:'',
component:AdminComponent,
children:[
{
path:'users',
component:UsersComponent
},
{
path:'roles',
component:RolesComponent
}
]
}
];
Lazy Child Route Architecture
flowchart TD
Application
Application --> Dashboard
Application --> Admin
Admin --> LazyBundle
LazyBundle --> Users
LazyBundle --> Roles
Child Routes with Route Guards
{
path: 'dashboard',
component: DashboardComponent,
canActivate: [
authGuard
],
children: [
{
path: 'payments',
component: PaymentsComponent,
canActivate: [
paymentGuard
]
}
]
}
Alternatively, use canActivateChild on the parent to protect all child routes.
Guard Flow
flowchart LR
User
User --> AuthGuard
AuthGuard --> Dashboard
Dashboard --> PaymentGuard
PaymentGuard --> Payments
Enterprise Banking Example
Modules
- Dashboard
- Accounts
- Payments
- Investments
- Loans
- Cards
- Statements
Architecture
flowchart TD
Dashboard
Dashboard --> Accounts
Dashboard --> Loans
Dashboard --> Payments
Dashboard --> Investments
Dashboard --> Statements
Accounts --> BankingAPI
Loans --> BankingAPI
Payments --> BankingAPI
Benefits
- Shared dashboard layout
- Reusable navigation
- Cleaner routing
- Better scalability
- Modular feature organization
Child Routes vs Separate Routes
| Child Routes | Separate Routes |
|---|---|
| Shared layout | Independent layouts |
| Nested URLs | Flat URLs |
| One parent component | No parent relationship |
| Better dashboard navigation | Better for unrelated pages |
| Shared navigation | Independent navigation |
Performance Best Practices
| Practice | Benefit |
|---|---|
| Keep routing hierarchy shallow | Easier maintenance |
| Lazy load large child features | Better startup performance |
| Protect parent routes with guards | Centralized security |
| Use child routes for shared layouts | Less duplication |
| Use route parameters | Dynamic navigation |
| Keep child components focused | Better readability |
Common Mistakes
Missing RouterOutlet
Without
<router-outlet></router-outlet>
child routes cannot be displayed.
Deep Route Nesting
Avoid excessive nesting.
Example
dashboard
↓
accounts
↓
transactions
↓
details
↓
history
Too much nesting makes applications difficult to navigate.
Duplicating Layouts
Don't recreate the same navigation and sidebar in every component.
Use a parent layout component.
Mixing Unrelated Features
Keep related pages together under the same parent route.
Forgetting Default Child Routes
Provide a default child route when appropriate to improve the user experience.
Best Practices
- Organize routes hierarchically.
- Keep parent components responsible for layout.
- Keep child components focused on feature content.
- Use nested
router-outlet. - Lazy load large feature areas.
- Protect parent routes using guards.
- Use default child routes for better navigation.
- Avoid deeply nested routing structures.
Advantages
| Feature | Benefit |
|---|---|
| Shared Layout | Less duplication |
| Nested Navigation | Better organization |
| Reusable Parent UI | Cleaner architecture |
| Lazy Loading | Better performance |
| Route Guards | Secure navigation |
| Enterprise Ready | Modular applications |
Top 10 Angular Child Routes Interview Questions
1. What are Angular Child Routes?
Answer
Child Routes are routes nested under a parent route, allowing hierarchical navigation and shared layouts.
2. Why use Child Routes?
Answer
They improve organization, reuse layouts, simplify navigation, and make large applications easier to maintain.
3. What is required inside a parent component?
Answer
The parent component must contain a <router-outlet> where Angular renders the active child route.
4. How are Child Routes configured?
Answer
Using the children property inside a route definition.
{
path: 'dashboard',
component: DashboardComponent,
children: [
{
path: 'accounts',
component: AccountsComponent
}
]
}
5. Can Child Routes have parameters?
Answer
Yes.
Child routes support dynamic route parameters just like top-level routes.
6. Can Child Routes be lazy loaded?
Answer
Yes.
Large feature areas commonly lazy load their child routes using loadChildren().
7. Can Child Routes use Route Guards?
Answer
Yes.
You can protect child routes using canActivate, canActivateChild, or other supported route guard types.
8. What is a default Child Route?
Answer
A default child route redirects users to a specific child page when they visit the parent route.
9. What is the difference between Child Routes and separate routes?
Answer
Child Routes share a parent layout and nested URL structure, while separate routes are independent.
10. What are the best practices?
Answer
- Use parent components for layouts.
- Add nested
router-outlet. - Lazy load large feature areas.
- Keep routing hierarchy shallow.
- Use route guards where appropriate.
- Avoid unnecessary nesting.
Interview Cheat Sheet
| Topic | Remember |
|---|---|
| Parent Route | Owns layout |
| Child Route | Nested feature |
| Configuration | children |
| RouterOutlet | Required |
| Default Route | redirectTo |
| Parameters | Supported |
| Lazy Loading | Supported |
| Route Guards | Supported |
| Shared Layout | Main advantage |
| Best Practice | Keep nesting manageable |
Summary
Angular Child Routes provide a clean and scalable way to organize related pages under a shared parent layout. By using the children property, nested router-outlet, default child routes, lazy loading, and route guards, developers can build modular applications with consistent navigation and reusable layouts. Child Routes are an essential part of enterprise Angular routing architecture and a common interview topic.
Key Takeaways
- ✔ Child Routes create hierarchical navigation.
- ✔ Parent components provide shared layouts.
- ✔ Nested
router-outletrenders child components. - ✔ Use the
childrenproperty to configure nested routes. - ✔ Child Routes support parameters, guards, and lazy loading.
- ✔ Use default child routes for improved navigation.
- ✔ Keep routing structures shallow and maintainable.
- ✔ Lazy load large feature areas.
- ✔ Reuse layouts instead of duplicating UI.
- ✔ Angular Child Routes are an important Angular Routing interview topic.
Next Article
➡️ 49-Route-Parameters-QA.md