Angular router Basic

The router enables us to navigate to different views on apps.  Here in this article, we will learn about the basic of angular 4 routing. We have to follow the certain step to enable routing in an Angular. In our app, we want to route to have the SearchComponent rendered when the url is / or root and the AboutComponent shown when the url is /about

Step 1: Add the base tag in  src/index.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Ngspotify</title>
  <base href="/">
   .....
   .....
</head>
<body>
  <app-root></app-root>
</body>
</html>

Most routing applications should add a <base> element to the index.html as the first child in the <head> tag to tell the router how to compose navigation URLs.

Step 2: Register router module in app.module.ts

import { BrowserModule } from '@angular/platform-browser';
...
import { AboutComponent } from './components/about/about.component';
import { NavbarComponent } from './components/navbar/navbar.component';
import { RouterModule, Routes } from '@angular/router';

const appRoutes: Routes = [
  {
    path: 'about',
    component: AboutComponent
  },
  {
    path: '',
    component: SearchComponent
  },
  {
    path: 'navbar',
    component: NavbarComponent
  }
];
@NgModule({
  declarations: [
    AppComponent,
    AboutComponent,
    NavbarComponent,
    SearchComponent
  ],
  imports: [
    BrowserModule,
    RouterModule.forRoot( appRoutes )
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

 

Step 3: RouterOutlet Directive

We’ve configured our application so if the user navigates to /about we want the AboutComponent shown or if they navigate to the root URL  / then we want the SearchComponent shown.

We need to add a directive called router-outlet somewhere in our template HTML. This directive tells Angular where it should insert each of those components in the route, we’ll add ours to the app.component.html, like so:

<navbar></navbar>
<div class="main">
    <div class="container">
        <router-outlet></router-outlet>
    </div>
</div>

 

For the navbar is  a component, in navbar.component.html we have add navigation code, we need to modify the tag in navbar.component.html as follow

<nav class="navbar navbar-inverse">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">ngSpotify</a>
    </div>
    <div id="navbar" class="collapse navbar-collapse">
      <ul class="nav navbar-nav">
        <li><a [routerLink]="['/']">Home</a></li>
        <li><a [routerLink]="['/about']">About</a></li>
      </ul>
    </div><!--/.nav-collapse -->
  </div>
</nav>