Overview Of Location Strategies And Child Route In Routing

In my previous article, we Have Learned About Routing. In this article, we will learn about location strategies and child routes.

In my previous article, we have learned about routing. In this article, we will learn about location strategies and child routes.

Location Strategies

Location Strategies are also called browser URL styles. Location strategy is a process to define how the URL will look in the browser.

Angular supports the concept of SPAs. In a single page application, the application will not send the URL to the server and will also not reload the page. In a single page application when the user requests for a new page the application will not send the URL to the server every time and the page will not reload every time. The URL is local in an Angular application. The Angular router makes navigation between the components and renders its view and updates the URL. The entire process happens on a local browser. In Angular, this process is managed by location strategies.

Types of Location Strategies

  1. HashLocation
  2. PathLocation

    Types of Location Strategies

HashLocation

You can use Hash location strategies by adding the following lines in the Appmodule.

Hashlocationstrategy RouterModule.forRoot(appRoutes, { useHash: true }

PathLocation

You can use PathLocation strategies by adding <base href=”/”> in the index.html page in your application. It is always better to use Path location strategies,

<base href="/">

Child/Nested Route

When a route uses another route inside it or a route within a route it is called Nested Route. The Child route is used for creating route hierarchy.

How to configure child routes?

Step 1

Create a Project with name AngularRouting using the below command,

ng new AngularRouting --routing

Step 2

Now, we have to add some component to the project. To add component open the integrated terminal in vs code. Open the terminal by pressing Ctrl, + and ~ button.

Now, add the below command to create components,

  • ng g c Home -it -is
  • ng g c About -it -is

Step 3 - Configure the Routes

First open the index.html file and add <base href="/"> in the head section. We add this file to construct the URL style while navigating the views.

Now, create a file app-routing.module.ts for configuring the different routes and create a constant route which is strongly typed to Route from the Router Package. The object has a path which is displayed in the URL and component which will render when we click on the corresponding component’s View.

App-routing.module.ts

  1. import { NgModule } from '@angular/core';  
  2. import { Routes, RouterModule } from '@angular/router';  
  3. import { HomeComponent } from './home/home.component';  
  4. import { AboutComponent } from './about/about.component';  
  5. const routes: Routes = [  
  6.    {path:'Home',component:HomeComponent},  
  7.    {path:'About',component:AboutComponent},  
  8. ];  
  9. @NgModule({  
  10.    imports: [RouterModule.forRoot(routes)],  
  11.    exports: [RouterModule]  
  12. })  
  13. export class AppRoutingModule { }  

Step 5

Now, add a file app.component.html and add the following lines.

App.component.html

  1. <div style="text-align:center">  
  2.    <h1>  
  3.       Welcome to {{ title }}!  
  4.    </h1>  
  5. </div>  
  6.    <nav>  
  7.       <a class="button" routerLink="/Home">Home</a>  
  8.       <a class="button" routerLink="/About">About </a>  
  9.    </nav>  
  10. <router-outlet></router-outlet>  
Step 6

Open App.module.ts file and import the component and AppRoutingModule.

App.module.ts

  1. import { BrowserModule } from '@angular/platform-browser';  
  2. import { NgModule } from '@angular/core';  
  3. import { AppRoutingModule } from './app-routing.module';  
  4. import { AppComponent } from './app.component';  
  5. import { HomeComponent } from './home/home.component';  
  6. import { AboutComponent } from './about/about.component';  
  7. @NgModule({  
  8. declarations: [  
  9.    AppComponent,  
  10.    HomeComponent,  
  11.    AboutComponent,  
  12. ],  
  13. imports: [  
  14.    BrowserModule,  
  15.    AppRoutingModule  
  16. ],  
  17. providers: [],  
  18.    bootstrap: [AppComponent]  
  19. })  
  20. export class AppModule { }  
Step 7

Let us Implement the child route in our Project. First, create a new component,

ng g c career -it -is

We want to create a page inside Home route. For adding route, Angular provides Children components. Let's open the app.routing.module.ts and add child routes,

  1. import { NgModule } from '@angular/core';  
  2. import { Routes, RouterModule } from '@angular/router';  
  3. import { HomeComponent } from './home/home.component';  
  4. import { AboutComponent } from './about/about.component';  
  5. import { CaererComponent } from './caerer/caerer.component';  
  6. import { ErrorComponent } from './error/error.component';  
  7. const routes: Routes = [  
  8. {path:"About",component:AboutComponent},  
  9. {path:"Home",component:HomeComponent,  
  10. children:  
  11. [  
  12.    {  
  13.        path:'Home/Career',component:CaererComponent  
  14.    }  
  15. ]},  
  16. ];  
  17. @NgModule({  
  18.    imports: [RouterModule.forRoot(routes)],  
  19.    exports: [RouterModule]  
  20. })  
  21. export class AppRoutingModule { }  
Now, make some change in the Home.component.ts and make the following changes,
  1. import {  
  2.     Component,  
  3.     OnInit  
  4. } from '@angular/core';  
  5. @Component({  
  6.     selector: 'app-home',  
  7.     template: `  
  8. <p>  
  9.    Home Page  
  10. </p>  
  11. <a routerLink="Home/Career">Career</a>  
  12.    <div>  
  13.       <router-outlet></router-outlet>  
  14.    </div>  
  15. `,  
  16.     styles: []  
  17. })  
  18. export class HomeComponent implements OnInit {  
  19.     constructor() {}  
  20.     ngOnInit() {}  
  21. }  

Now, run the project and check,

Run the project

Summary

In this article, we have learned about Location Strategies and child routes.