What is Angular Router?#
The Angular Router is a built-in module in Angular that enables Single-page applicationsA single-page application (SPA) is a web application that dynamically updates content on a single page without requiring full page reloads. It improves performance and user experience by fetching and rendering only the necessary data using JavaScript frameworks like Angular, React, or Vue. to navigate between views without reloading the page. It listens to the browser’s URL and dynamically loads components based on defined routes.
URLs consist of a domain name and a route definition, known as a path.
A path is a JavaScript object that the server uses to access a specific resource in the database. When the server serves our application, Angular will grab the path from the URL and match it against any valid paths we’ve set up.
Essentially, we set a key/value relationship with a path like /blog
as the key and the desired page as the value
.
This lets users move around your app easily and go straight to the page they want, without starting from the home component.
Routing enables support for common browser behaviors like forward/back arrows and page bookmarking.
Router also contains tools for advanced behaviors like multiple router outlets, different path matching strategies, easy access to route parameters, and route guards to protect components from unauthorized access.
Routing module and RouterOutlet#
Routing modules are special parts of an Angular app that set up new routes and help control how users move between pages. All routing modules have the suffix -routing
after their name, which is automatically added by Angular CLI.
Each routing module controls how pages change for the part of the app it is paired with — both parts usually have the same name. For example, the routing behavior for our home
module would be in the routing module home-routing
.
Here’s an example of a routing module for our home
module, called home-routing.module.ts
:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeMainComponent } from './home-main/home-main.component';
const routes: Routes = [
{ path: '', component: HomeMainComponent }
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class HomeRoutingModule { }
You can find our routes in the routes
array variable. Each element of the routes
array represents the route to a single component view.
The elements consist of two parts, the path
property that provides a URL path and the component
property that defines which component will be loaded at the provided path.
In this case, we use an empty string (which means a forward slash) to show that this is the homepage. It will load when someone just types the website’s main address.
We then enter the name of the component Angular should fetch as our homepage, HomeMainComponent
.
We can also use redirectto
to direct empty path URLs to another page if we’d prefer users land elsewhere.
Next, we’ll need to remove HomeMainComponent
from the exports of HomeModule
. Because we’re using routing, we don’t need to export the component anymore. The Router will load the right component when a user visits the matching page.
Finally, we’ll replace the contents of the app.component.html
file with the line:
<router-outlet></router-outlet>
Here, <router-outlet>
acts as a placeholder for the component. Instead of setting a specific component, our template will show whatever component matches the URL that was typed in. By using this placeholder, we don’t have to export the component. Instead, we can export the module.