# Understand Angular Router

Jun 22nd, 2017 - written by Kimserey with .

Today we will see how to use the Angular Router. The router allows us to define routes which are transformed to urls which are then understood by the browser. Having routes allows us to create different categories and access points to our website.

1. Define routes
2. Router outlet
3. Special routes
4. Data and ActivatedRoute
5. Resolve guard
6. CanXXX guards

## 1. Define routes

To start, we need to import the RouteModule and the Routes type from the Angular router.

1
import { RouterModule, Routes } from '@angular/router';


The routes are defined via constant and then injected into the router module using either forRoot or forChild.

forRoot is used to define routes on the main module and forChild is used to define routes on the child modules

For example we can define two routes which we add to the main module:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const routes: Routes = [
{
path: 'home',
component: MainPageComponent,
},
{
path: '',
redirectTo: 'home',
pathMatch: 'full'
}
];

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


Here two routes are defined, /home which will display HomePageComponent and / which will redirect to home. The routes are then added to the route module with RouterModule.forRoot(routes).

## 2. Router outlet

The HomePageComponent will be displayed below the router-outlet tag. Our main AppComponent template is as followed:

1
2
3

<router-outlet></router-outlet>


Component is displayed after router-outlet. routerLink is used to specify the link, it can be specified as a relative path or full path. routerLink="test" will add to the current path while routerLink="/test" will replace the path. For example if the current path is /hello, ="test" will result in /hello/test while ="/test" will result in /test. routerLinkActive="active" defines the class which will be added to the element when the route is active.

The router-outlet accepts a name. This can be used to display components side by side. For example we define a route my-page and two outlet first and second, a possible route could be as followed:

1
/my-page/(first:x/y/z//second:a/b/c)


The two outlets maintain relative paths first:x/y/z and second:a/b/c, separated by a double slash //. This is very powerful as it gives a unique route for any permutation of the side by side components. The routeLink would be defined like so:

1
2
3
4
5
6
7
8
myRoute = [
'/one-page',
{
outlets: {
first: ['x','y','z'],
second: ['a','b','c']
}
}];


## 3. Special routes

### 3.1 Redirect

In 1) we saw the following route:

1
2
3
4
5
{
path: '',
redirectTo: '/home',
pathMatch: 'full'
}


redirectTo will redirect to /home. In order to redirect properly, we need to specify the pathMatch. There are two types prefix and full. Prefix being the default we only need to specify when we want the matching to be full. full will match the full remaining path while prefix will match the path which starts with the path given. '' being the prefix of all paths, if we set prefix it will always redirect.

### 3.2 Wildcard

Another special route is the wildcard. It can be defined using the path path:'**'. For example here we can define the following under the one-page route:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
path: 'one-page',
component: OnePageComponent,
children: [
{
path: '',
component: SecondPageComponent
},
{
path: '**',
redirectTo: '',
pathMatch: 'full'
}
]
},


Any route after one-page that aren’t '' will be redirected to '' which will then result in /one-page.

## 4. Data and ActivatedRoute

### 4.1 Static data

Static data can be configured in the route.

1
2
3
4
5
6
7
{
path: '',
component: MyComponent,
data: {
hello: 'world'
}
}


Then in MyComponent, we can access those data via the ActivatedRoute. The data are held in an observable. We can access it as followed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Component({
template: 
<strong>{{ data$| async }}</strong>  }) export class MyComponent implements OnInit { data$: Observable<any>;

constructor(private route: ActivatedRoute) {}

ngOnInit() {
this.data$= this.route.data.pluck('hello'); } }  ### 4.2 Parameters Similarly to the data, parameters can be extracted from the route using the notation :parameter and accessed via the ActivatedRoute: 1 2 3 4 { path: ':id', component: MyComponent, }  1 2 3 4 5 6 7 8 9 10 11 12 export class MyComponent implements OnInit { data$: Observable<string>;

constructor(private route: ActivatedRoute) {}

ngOnInit() {
this.data$= this.route .paramMap .filter(m => m.has('id')) .map(m => m.get('id')); } }  ## 5. Resolve guard Other than resolving static data using data, there are instances where it is desired to retrieve asynchronous data when accessing a page. In order to achieve that we can use the resolve guard. It is an interface to implement on a service: 1 2 3 4 interface Resolve { resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<T>|Promise<T>|T }  The route given is the current route activated, giving access to the data, component and params of the current route. The state is a tree representing the snapshot at this instant of the whole route tree. It can be used to traverse all child routes activated. The return type accepts an Observable, a Promise or a concrete type T. For example we could have a title which requires dynamic data: 1 2 3 4 5 6 7 8 9 @Injectable() export class TitleResolver implements Resolve<string> { resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<string> { return Observable .of('An async title') .delay(5000); } }  Then we will need to register this service in the module: 1 2 3 4 5 6 7 8 @NgModule({ imports: [...], declarations: [...], providers: [ TitleResolver ] }) export class MyModule { }  We can then add the resolver into the route configuration and define a variable myTitle to place the result in. We will then be able to access this variable from the data. 1 2 3 4 5 6 7 8 9 const routes: Routes = [ { path: 'my-route', component: MyComponent, resolve: { myTitle: TitleResolver } } ];  This will ensure that the title is resolved before the compononent is shown. We can then use the resolved value by plucking it out of the data. We can be sure that at least one value will be resolved from the myTitle variable. 1 2 3 4 5 6 7 8 9 10 11 export class OnePageComponent implements OnInit { title$: Observable<string>;

constructor(private route: ActivatedRoute) { }

ngOnInit() {
this.title\$ = this.route
.data
.pluck('myTitle');
}
}


## 6. CanXXX guards

There are three other guards available, canActivate, canActivateChild and canLoad. canActivate and canActivateChild specify whether the route ca be activated. A common scenario is authentication verification which will be performed in the guard and if the user isn’t authenticated, it will redirect to the login page.

canActivate is used to decide whether the user is able to access the current route while canActivateChild decides if the user is able to access the child of the route where the guard is placed. Another difference is in the arguments given by the interfaces. For canActivate, we have access to the route:

1
2
3
export interface CanActivate {
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean;
}


While with canActivateChild, we have access to the child:

1
2
3
export interface CanActivateChild {
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean;
}


Therefore the main difference is that in one we use the current route where the guard is placed on to decide whether the user is allowed to activate the route while in the other case, we use the child route where the guard is placed to decide.

We have protected the route with canActive and canActiveChild but the modules which are protected are still imported into the app module therefore still loaded. There are instances where the module is very specific and targeted to a limited amount of users. In those cases, we can lazy load the module which will result in loading dynamically the module when the route is hit. This can be achieved using loadChildren and providing the path of the file containing the module to lazily load followed by #MyModule.

1
2
3
4
{
path: 'my-lazy-route',
}


Then we can remove the module import in the app module. The module will no longer be loaded on boot but only loaded on navigation to my-lazy-route. In this case, canActive will happen after we load the module. But what we actually want isn’t to load and check but to directly prevent the module to be loaded for unauthorized users. This is where we can use canLoad which will prevent the module to be loaded.

1
2
3
4
5
{
path: 'my-lazy-route',
}


This canLoad is a service which must implement the CanLoad interface which allow to decide, based on the route, if the module should be loaded or not.

1
2
3