What is Angular Router?#
The Angular Router is an importable package built-in to Angular 2+ by default. It’s used to build Single Page Applications with multiple views that can be navigated by URL, known as “routing”.
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 allows users to easily navigate your app and visit the page they want without having to start at 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 Angular modules that define new routes and help configure the router. All routing modules have the suffix -routing
after their name, which is automatically added by Angular CLI.
Every routing module sets the routing behavior for a paired module with the same base 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 enter an empty string (interpreted as a forward slash) to indicate that this component is our homepage and should load if someone just enters the domain name. 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
. This use of routing means we’re no longer exporting the component and instead let Router take care of loading the component if a user visits the route.
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 defining a component, our template will simply pull whatever component is rendered with the passed URL path. By using this placeholder, we don’t have to export the component. Instead, we can export the module.