In this section, we will learn how to set up routing in an Angular application. Routing allows us to navigate between different views or components in our application. This is essential for creating single-page applications (SPAs) where the user can navigate without reloading the entire page.

Key Concepts

  1. Router Module: The Angular Router module is used to configure the routes.
  2. Routes: Routes define the mapping between a URL and a component.
  3. Router Outlet: A placeholder in the template where the routed component will be displayed.
  4. Router Link: A directive to link to different routes.

Steps to Set Up Routes

  1. Importing the Router Module

First, we need to import the RouterModule and Routes from @angular/router in our AppModule.

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule, Routes } from '@angular/router';

import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';

const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'about', component: AboutComponent }
];

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    AboutComponent
  ],
  imports: [
    BrowserModule,
    RouterModule.forRoot(routes)
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

  1. Defining Routes

In the routes array, we define the paths and the corresponding components. For example:

  • { path: '', component: HomeComponent }: This route maps the root URL (/) to the HomeComponent.
  • { path: 'about', component: AboutComponent }: This route maps the /about URL to the AboutComponent.

  1. Adding Router Outlet

In the AppComponent template, we need to add a <router-outlet> directive. This is where the routed component will be displayed.

<!-- app.component.html -->
<nav>
  <a routerLink="/">Home</a>
  <a routerLink="/about">About</a>
</nav>
<router-outlet></router-outlet>

  1. Using Router Link

We use the routerLink directive to create navigation links. When a user clicks on these links, the router will navigate to the corresponding route.

<!-- app.component.html -->
<nav>
  <a routerLink="/">Home</a>
  <a routerLink="/about">About</a>
</nav>
<router-outlet></router-outlet>

Practical Example

Let's create a simple Angular application with two components: HomeComponent and AboutComponent.

  1. Generate Components

Use Angular CLI to generate the components:

ng generate component home
ng generate component about

  1. Update AppModule

Update the AppModule to include the routes and import the RouterModule.

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule, Routes } from '@angular/router';

import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';

const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'about', component: AboutComponent }
];

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    AboutComponent
  ],
  imports: [
    BrowserModule,
    RouterModule.forRoot(routes)
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

  1. Update AppComponent Template

Update the AppComponent template to include navigation links and the router outlet.

<!-- app.component.html -->
<nav>
  <a routerLink="/">Home</a>
  <a routerLink="/about">About</a>
</nav>
<router-outlet></router-outlet>

  1. Add Content to Components

Add some content to the HomeComponent and AboutComponent templates.

<!-- home.component.html -->
<h1>Welcome to the Home Page</h1>
<p>This is the home page of our Angular application.</p>
<!-- about.component.html -->
<h1>About Us</h1>
<p>This is the about page of our Angular application.</p>

Practical Exercise

Task

  1. Create a new Angular component named ContactComponent.
  2. Add a route for the ContactComponent with the path contact.
  3. Update the navigation menu to include a link to the ContactComponent.
  4. Add some content to the ContactComponent template.

Solution

  1. Generate the ContactComponent:
ng generate component contact
  1. Update the AppModule to include the new route:
const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'about', component: AboutComponent },
  { path: 'contact', component: ContactComponent }
];
  1. Update the AppComponent template to include the new link:
<nav>
  <a routerLink="/">Home</a>
  <a routerLink="/about">About</a>
  <a routerLink="/contact">Contact</a>
</nav>
<router-outlet></router-outlet>
  1. Add content to the ContactComponent template:
<!-- contact.component.html -->
<h1>Contact Us</h1>
<p>This is the contact page of our Angular application.</p>

Summary

In this section, we learned how to set up routing in an Angular application. We covered the following key points:

  • Importing the RouterModule and defining routes.
  • Using the <router-outlet> directive to display routed components.
  • Creating navigation links with the routerLink directive.

By following these steps, you can create a navigable Angular application with multiple views. In the next section, we will explore route parameters and how to use them to pass data between routes.

© Copyright 2024. All rights reserved