Routing in angular 4

Routing in angular 4

Routing is used to navigate one component to another component without ever change the URL. Adding routing allow the user to go straight into certain aspects of the application. This is very useful. it can keep your application linkable and bookmark and allow users to share links with others.

configure Routing

The Base URL tag must be set within  <head> tag of index.html

<base href="/">

The <base href="/"> tell the Angular router what is the static part of the URL. Then only Router modifies the remaining part of the URL.‚Äč

Route Definition object

The Route type is an array route, this is define the routing for the application. we can set up the expected paths.

Each route have different attribute. The common attribute are

Path - URL is shown in the browser when application in the specific route
component - pointing the specific component for the specific route
redirectTo - redirect route if needed. each route have either component or redirect attribute defined in the routing.
pathMatch -  optional property that defaults to 'prefix' determines whether to match full URLs or just the beginning. When defining a route with empty path string set pathMatch to 'full', otherwise it will match all paths
children - array of route definitions objects representing the child routes of this route 


import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app.routes';
import { AppComponent } from './app.component.ts';
import ComponentOne from './component-one';
import ComponentTwo from './component-two';

  imports: [ BrowserModule, AppRoutingModule],
  declarations: [ AppComponent, ComponentOne, ComponentTwo ],
  providers: [],
  bootstrap: [ AppComponent ]
export class AppModule {}


import {Component} from '@angular/core';
  selector: 'app',
  template: `
      <a routerLink="/component-one">Component One</a>
      <a routerLink="/component-two">Component Two</a>

    <div style="color: green; margin-top: 1rem;">Outlet:</div>
    <div style="border: 2px solid green; padding: 1rem;">


export class AppComponent { }


import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import ComponentOne from './component-one';
import ComponentTwo from './component-two'; export const routes: Routes = [
  { path: '', redirectTo: 'component-one', pathMatch: 'full' },
  { path: 'component-one', component: ComponentOne },
  { path: 'component-two', component: ComponentTwo }
  imports: [RouterModule.forRoot(appRoutes)],
  exports: [RouterModule]
export class AppRoutingModule {}


import {Component} from '@angular/core';
  selector: 'component-one',
  template: 'Component One'

export default class ComponentOne { }


import {Component} from '@angular/core';

  selector: 'component-two',
  template: 'Component Two'

export default class ComponentTwo {}


import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app.module';



<!DOCTYPE html>



    <title>Angular 2 - Basic router example</title>

    <script src=""></script>

    <script src=""></script>

    <script src="system.config.js"></script>

    <!-- Set the base href, demo only! In your app: <base href="/"> -->

    <script>document.write('<base href="' + document.location + '" />');</script>










Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed