↓Skip to main content

Angular Routing: Routing, Navigation, and Route Parameters in Angular

·316 words·2 mins
Programing Web development Angular Frontend Development
Anurag Konduru
Author
Anurag Konduru
Software Engineer (Front-End Developer/Linuxuser/Software Explorer)
Learning Angular - This article is part of a series.

Let’s dive into the world of routing in Single-Page Applications (SPAs) ie., Angular Routing! Lets take a look at basics of defining routes, navigating between components, and leveraging route parameters for dynamic content.

Setting Up Routing
#

  1. Import RouterModule:
  • In your main application module (often AppModule), import RouterModule from @angular/router.
    import { NgModule } from '@angular/core';
    import { RouterModule, Routes } from '@angular/router';
    
    @NgModule({
      // ...
      imports: [
        // ...
        RouterModule.forRoot(routes)
      ],
      // ...
    })
    export class AppModule { }
    
  1. Define Routes:
  • Create an array of Objects as Routes specifying URL paths and their corresponding components.
    const routes: Routes = [
    { path: '', component: HomeComponent },
    { path: 'about', component: AboutComponent },
    { path: 'products/:id', component: ProductDetailsComponent } // Route with parameter
    ];
    
  • path: Defines the URL pattern (can include static paths and dynamic segments using :).
  • component: The Angular component to display for this route.
  1. Configure RouterModule:
  • Use RouterModule.forRoot(routes) in your AppModule to configure routing for the application.

Navigation #

There are two main ways to navigate between routes in your Angular app:

  1. Declarative Navigation:
  • Use the routerLink directive in your component’s template to create clickable links.
  • Bind the routerLink to the desired route path.
    <a routerLink="/about">About Us</a>
    
  1. Programmatic Navigation:

    • Inject the Router service into your component.
    • Use the navigate method to navigate to a specific route.
    constructor(private router: Router) {}
    
    navigateToAbout() {
      this.router.navigate(['/about']);
    }
    

Route Parameters
#

Route parameters allow you to capture dynamic segments from the URL and use them in your component.

  1. Define Route with Parameter:

    • Define with custom :Identifier in the path
    { path: 'products/:id', component: ProductDetailsComponent }
    
  2. Access Parameter in Component:

  • Inject the ActivatedRoute service into your component.

  • Use the paramMap observable to access the route parameters.

    constructor(private route: ActivatedRoute) {}
    productId='';
    ngOnInit() {
    this.activeRoute.params.subscribe((params:Params)=>{
        this.productId=params['id'];
    });
    }
    

Note: This is just a starting point! Angular Routing offers many more features like child routes, lazy loading, and route guards.

Learning Angular - This article is part of a series.

Related

Angular Service & Dependency Injection
·153 words·1 min
Programing Web development Angular Frontend Development
Angular Service & Dependency Injection # Services: # Services in Angular serve as repositories for reusable code, facilitating storage and accessibility throughout the application.
Angular Directives: Guiding the DOM with Instructions
·687 words·4 mins
Programing Web development Angular Frontend Development
Directives: # Directives are instructional elements within the DOM.
Angular Databinding: Connecting TypeScript and HTML
·285 words·2 mins
Programing Web development Angular Frontend Development
Data Binding # When it comes to data binding in Angular, the synergy between TypeScript and HTML is facilitated through various techniques: