Introduction to routing
Chapter objectives
- โ๏ธUnderstanding routing with AngularLearn how routing works in Angular.
- โ๏ธDefine routesDefine routes in your application.
What is routing?
Routing is the process of determining what content to display based on the browserโs URL. Angular is a Single Page Application (SPA) framework. By using the Router API, the application can display different content without completely reloading the page.
The project currently only displays the TaskListComponent. The Router will enable switching between this component and the new TaskFormComponent.
The Router API is provided by the @angular/forms
package.
Itโs installed in any new Angular project by default and you cand find it in the pckage.json file.
Router-outlet directive
The Router API provides a directive called router-outlet
. outed content will be displayed. When the user navigates to a different route, the content of the router-outlet
is replaced with the content of the new route.
The router-outlet
directiveโs only role is to mark the location in the HTML Template where the content corresponding to the URL you defined will be displayed.
๐ Instructions
-
Modify the
src/app/app.component.html
file.<header><h1>Angular Legacy course</h1></header><main class="container pt-4"><router-outlet /></main>
In the above case, the router-outlet
is placed between the <main>
tags. This means that you will continue to display the <header>
on top of all pages and the configured content will change based on the URL you navigate to.
Define routes
To display the desired content based on the URL, you need to add a path
to each of your component
s in your routes
array:
{ path: 'add-task', component: TaskFormComponent }
The path is therefore the URL that the user will access. For example, the path /add-task
means that the user will navigate to the URL http://localhost:4200/add-task
.
The provided component is the component that will be displayed in the router-outlet
placeholder when the user accesses this URL.
The route definition resides in the app-routing.module.ts
file.
๐ Instructions
-
Modify the
src/app/app-routing.module.ts
file.app-routing.module.ts import { NgModule } from "@angular/core";import { Routes, RouterModule } from "@angular/router";import { TaskListComponent } from "./task-list/task-list.component";import { TaskFormComponent } from "./task-form/task-form.component";const routes: Routes = [{ path: "", component: TaskListComponent },{ path: "add-task", component: TaskFormComponent },];@NgModule({imports: [RouterModule.forRoot(routes)],exports: [RouterModule],})export class AppRoutingModule {}
Add links to navigate between routes
You need to add links to trigger navigation between different routes. You could manually modify the URL in the browser, but users expect to have clickable links to navigate between pages.
The HTML <a>
tag is used to create links and is associated with the routerLink directive to use Angular routing. This directive takes the path of the route to navigate to as a value.
๐ Instructions
-
Modify the
src/app/app.component.html
file.app.component.html <header><h1 class="navbar-brand fw-bold">Angular Legacy course</h1><a class="btn btn-light" routerLink="/">Task List</a><a class="btn btn-light" routerLink="/add-task">Add new task</a></header><main class="container pt-4"><router-outlet /></main> -
Click on both links to see the content of
TaskListComponent
andTaskFormComponent
displayed alternately at the location defined by therouter-outlet
.