In this section, we will explore the concept of child routes in Angular. Child routes allow you to create nested routes within a parent route, enabling more complex and hierarchical navigation structures in your Angular applications.
Key Concepts
- Parent and Child Routes: Understanding the relationship between parent and child routes.
- Nested Routes: How to define and configure nested routes.
- Router Outlet: Using multiple router outlets to display nested views.
- Route Configuration: Setting up child routes in the route configuration.
Understanding Parent and Child Routes
Parent routes are the main routes in your application, while child routes are nested within these parent routes. This hierarchical structure allows you to create complex navigation patterns.
Example Structure
In this example, /parent
is the parent route, and /parent/child1
and /parent/child2
are child routes.
Setting Up Child Routes
To set up child routes, you need to define them in the route configuration of the parent module.
Step-by-Step Guide
- Create Components: Create the parent and child components.
- Define Routes: Define the parent and child routes in the route configuration.
- Configure Router Outlet: Use
<router-outlet>
in the parent component to display child components.
Example
1. Create Components
Create a parent component and two child components.
ng generate component parent ng generate component parent/child1 ng generate component parent/child2
2. Define Routes
Define the routes in the app-routing.module.ts
file.
import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; import { ParentComponent } from './parent/parent.component'; import { Child1Component } from './parent/child1/child1.component'; import { Child2Component } from './parent/child2/child2.component'; const routes: Routes = [ { path: 'parent', component: ParentComponent, children: [ { path: 'child1', component: Child1Component }, { path: 'child2', component: Child2Component } ] } ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule { }
3. Configure Router Outlet
Use <router-outlet>
in the parent component's template to display the child components.
<!-- parent.component.html --> <h2>Parent Component</h2> <nav> <a routerLink="child1">Child 1</a> <a routerLink="child2">Child 2</a> </nav> <router-outlet></router-outlet>
Practical Example
Let's see a complete example with the parent and child components.
Parent Component
// parent.component.ts import { Component } from '@angular/core'; @Component({ selector: 'app-parent', templateUrl: './parent.component.html', styleUrls: ['./parent.component.css'] }) export class ParentComponent { }
<!-- parent.component.html --> <h2>Parent Component</h2> <nav> <a routerLink="child1">Child 1</a> <a routerLink="child2">Child 2</a> </nav> <router-outlet></router-outlet>
Child 1 Component
// child1.component.ts import { Component } from '@angular/core'; @Component({ selector: 'app-child1', templateUrl: './child1.component.html', styleUrls: ['./child1.component.css'] }) export class Child1Component { }
Child 2 Component
// child2.component.ts import { Component } from '@angular/core'; @Component({ selector: 'app-child2', templateUrl: './child2.component.html', styleUrls: ['./child2.component.css'] }) export class Child2Component { }
Practical Exercise
Task
- Create a new parent component named
dashboard
. - Create two child components named
overview
anddetails
. - Set up child routes for
overview
anddetails
under thedashboard
route. - Add navigation links in the
dashboard
component to navigate tooverview
anddetails
.
Solution
- Create Components
ng generate component dashboard ng generate component dashboard/overview ng generate component dashboard/details
- Define Routes
// app-routing.module.ts import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; import { DashboardComponent } from './dashboard/dashboard.component'; import { OverviewComponent } from './dashboard/overview/overview.component'; import { DetailsComponent } from './dashboard/details/details.component'; const routes: Routes = [ { path: 'dashboard', component: DashboardComponent, children: [ { path: 'overview', component: OverviewComponent }, { path: 'details', component: DetailsComponent } ] } ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule { }
- Configure Router Outlet
<!-- dashboard.component.html --> <h2>Dashboard Component</h2> <nav> <a routerLink="overview">Overview</a> <a routerLink="details">Details</a> </nav> <router-outlet></router-outlet>
Summary
In this section, we learned about child routes in Angular. We covered the following key points:
- Understanding the relationship between parent and child routes.
- Setting up nested routes in the route configuration.
- Using
<router-outlet>
to display child components.
By mastering child routes, you can create more complex and hierarchical navigation structures in your Angular applications. In the next section, we will explore route guards to protect routes and control access based on certain conditions.
Angular Course
Module 1: Introduction to Angular
- What is Angular?
- Setting Up the Development Environment
- Angular Architecture
- First Angular Application
Module 2: Angular Components
- Understanding Components
- Creating Components
- Component Templates
- Component Styles
- Component Interaction
Module 3: Data Binding and Directives
- Interpolation and Property Binding
- Event Binding
- Two-Way Data Binding
- Built-in Directives
- Custom Directives
Module 4: Services and Dependency Injection
Module 5: Routing and Navigation
Module 6: Forms in Angular
Module 7: HTTP Client and Observables
- Introduction to HTTP Client
- Making HTTP Requests
- Handling HTTP Responses
- Using Observables
- Error Handling
Module 8: State Management
- Introduction to State Management
- Using Services for State Management
- NgRx Store
- NgRx Effects
- NgRx Entity
Module 9: Testing in Angular
Module 10: Advanced Angular Concepts
- Angular Universal
- Performance Optimization
- Internationalization (i18n)
- Custom Pipes
- Angular Animations