Routing is a fundamental feature in Angular that allows you to navigate between different views or components in your application. In this section, we will cover the basics of setting up routes in an Angular application.
Key Concepts
- Router Module: The Angular Router module is used to configure the routes.
- Routes: Routes define the mapping between a URL and a component.
- Router Outlet: A placeholder in the template where the routed component will be displayed.
- Router Link: A directive to link to different routes.
Steps to Set Up Routes
- Importing the Router Module
First, you need to import the RouterModule
and Routes
from @angular/router
in your 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 { }
- Defining Routes
In the routes
array, each route is an object with two properties:
path
: The URL path.component
: The component to display when the path is matched.
const routes: Routes = [ { path: '', component: HomeComponent }, { path: 'about', component: AboutComponent } ];
- Adding Router Outlet
In your main template (usually app.component.html
), add the <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>
- Using Router Link
Use the routerLink
directive to create navigation links.
<!-- 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
.
- Generate Components
Use Angular CLI to generate the components:
- Update AppModule
Update app.module.ts
to include the routes and import the components.
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 { }
- Update Templates
Update app.component.html
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>
Update home.component.html
and about.component.html
with some content.
Practical Exercise
Exercise
- Create a new Angular component named
ContactComponent
. - Add a new route for the
ContactComponent
with the pathcontact
. - Update the navigation menu to include a link to the Contact page.
- Add some content to
contact.component.html
.
Solution
- Generate the
ContactComponent
:
- Update
app.module.ts
to include the new route:
const routes: Routes = [ { path: '', component: HomeComponent }, { path: 'about', component: AboutComponent }, { path: 'contact', component: ContactComponent } ];
- Update
app.component.html
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>
- Add content to
contact.component.html
:
Summary
In this section, we learned how to set up basic routing in an Angular application. We covered:
- Importing the
RouterModule
and defining routes. - Using the
<router-outlet>
directive to display routed components. - Creating navigation links with the
routerLink
directive.
In the next section, we will explore how to handle route parameters to create dynamic routes.
Angular 2+ Course
Module 1: Introduction to Angular
Module 2: TypeScript Basics
- Introduction to TypeScript
- TypeScript Variables and Data Types
- Functions and Arrow Functions
- Classes and Interfaces