This article is in continuation of our Angular 2 series. If you haven’t had a look at the previous articles, then check-out this link. This will be the last part of theAngular 2 series where we’ll talk about Routing. I’ll split this article into 2 parts since routing is such a broad topic. Every application consists of sets of pages which can be accessed through links, menu-items etc. In Angular 2, routing enables navigation between these discrete application views. For implementing routing in our application, we need to import “RouterModule” of Angular 2 like how we imported “HttpModule” for making server calls. As I have already mentioned, Angular 2 is fully modularized, every feature has its own module. Here is the import statement that I’ve added in our application's root module i.e. app.module.ts file.
- import { RouterModule, Routes } from '@angular/router';
To set up with Angular 2 routing, we need to specify the application base path which is an HTML element needed to be placed beneath <head> element. By default, Angular 2 router follows HTML5 pushState style URLs. So, inside our index.html file, I’ve added the below line under the head element to tell Angular router how to compose navigation URLs.
- <base href="/"/>
The next step is to set up the router-outlet, where navigated View contents should be displayed. Router-Outlet is a built in Angular directive defined in RouterModule. It acts as a placeholder for rendering navigated View contents. I’m placing this outlet inside my app.component.ts file which is our root component for our application. Here is the updated app.component.ts file,
- import { Component } from '@angular/core';
- @Component({
- selector: 'my-app',
- template: <nav class="navbar navbar-inverse"> <div class='container-fluid'>
- <a class='navbar-brand'>{{orgName}}</a>
- <ul class='nav navbar-nav'>
- <li><a>Home</a></li>
- </ul>
- </div>
- </nav>
- <div class='container'>
- <router-outlet></router-outlet>
- </div>
- </div>`
- })
- export class AppComponent {
- orgName: string = "Angular2 Series";
- }
Our navigation bar has just one menu with name “HOME”. The last part is configuring our route for the application. A routed Angular application has one singleton instance of the Router service. When browser’s URL changes, the router looks for the corresponding route & loads the respective component to display. By default, a RouterModule has no route until you configure it. For configuring route, I’m adding a new file at root level with name “app.route-config.ts”. Below is the code snippet for the same.
- import {Routes } from '@angular/router';
- import{WelcomeComponent} from './welcome/welcome.component';
- export const routeConfig: Routes = [
- {
- path: 'welcome',
- component: WelcomeComponent
- }
- ]
NOTE
Welcome component is an empty class with template displaying the message “This is our welcome page”.
Now that we’ve configured our route for the application, we are just left with set up of Routing in our application and to do that, we need to simply pass this routeConfig collection to RouterModule.forRoot() function. Here is the updated code snippet for app.module.ts file.
- /** Importing the angular modules from namespace */
- import { NgModule } from '@angular/core';
- import { FormsModule } from '@angular/forms';
- import { BrowserModule } from '@angular/platform-browser';
- import { HttpModule } from '@angular/http';
- import { RouterModule, Routes } from '@angular/router';
- /** Importing the AppComponent from the application's app folder */
- import { AppComponent } from './app.component';
- import 'rxjs/Rx';
- import { EmpModule } from './emp/emp.module';
- import { routeConfig } from './app.route-config';
- import { WelcomeComponent } from './welcome/welcome.component';
- @NgModule({
- imports: [BrowserModule, FormsModule, HttpModule, EmpModule, RouterModule.forRoot(routeConfig)],//other modules whose exported classes are needed by component templates declared in this module.
- declarations: [AppComponent, WelcomeComponent],// the view classes that belong to this module. Angular has three kinds of view classes: components, directives, and pipes.
- bootstrap: [AppComponent]//the main application view, called the root component, that hosts all other app views. Only the root module should set this bootstrap property.
- })
- export class AppModule { }
Now, if you try running the application, you’ll find the below snapshot.

If you try to access the welcome page by updating browser’s URL with “/welcome”, you’ll get the below snapshot.

Until now, we were directly updating the browser’s Url to navigate to our View. But in real applications, you’ll be providing the user with menu or links, on clickinf of which the navigation should occur. In Angular2 this could be achieved through RouterLink directive (an inbuilt angular directive). This directive could be applied on links, or for that matter, on any HTML elements on click of which you want navigation to happen. Here is the updated code fragment from app.component.ts file.
- <nav class='navbar navbar-default'>
- <div class='container-fluid'> <a class='navbar-brand'>{{orgName}}</a>
- <ul class='nav navbar-nav'>
- <li><a routerLink="welcome">Home</a></li>
- </ul>
- </div>
- </nav>
NOTE
routerLink value must match the routeConfig path value.
Try refreshing the page and then click on “Home” menu. It should redirect you to the welcome View.
We can also configure the default route for the application as soon as the application starts. For instance, in our application, I want as soon as the application starts user should be redirected to “Home” page. For doing that, I need to configure my route with an empty path. Below is the code snippet for the same:
app.route-config.ts file
- import { Routes } from '@angular/router';
- import { WelcomeComponent } from './welcome/welcome.component';
- export const routeConfig: Routes = [
- {
- path: 'welcome',
- component: WelcomeComponent
- },
- {
- //default route
- path: '',
- component: WelcomeComponent
- }
- ]







Bryian TanPosted Mar 2, 2017, 8:58 AM
Got it. thanks for sharing.
Bryian TanPosted Mar 1, 2017, 4:54 PM
I clicked on the empid on emp-list page and the application redirect the page to emp-list/emp-details. Is the Employee Details Component page suppose to be blank?