close
The Wayback Machine - https://web.archive.org/web/20240716081226/https://www.geeksforgeeks.org/routing-in-angular-9-10/
Open In App

Routing in Angular 9/10

Last Updated : 17 Jun, 2021
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Routing in Angular allows the users to create a single-page application with multiple views and allows navigation between them. Users can switch between these views without losing the application state and properties.

Approach:

  • Create an Angular app that to be used.
  • Create the navigation links inside the app component and then provide the “routerLink” directive to each route and pass the route value to “routerLink” directive.
  • Then add the routes to the routing.module.ts file and then import the routing.module.ts into the app.module.ts file.

Syntax:

  • HTML:
<li><a routerLink="/about" >About Us</a></li>
<router-outlet></router-outlet>
  • TS:

 { path: 'about', component: AboutComponent }

Example: We are going to create a simple angular application that uses angular routing. So first, we create an Angular app by running the below command in CLI.

ng new learn-routing

Then we are creating simple navigation that allows us to navigate between the different components, and we have created some components as well, so users can switch between these components using routing.

app.component.html




<span>
    <ul>
        <li><a routerLink="/" >Home</a></li>
        <li><a routerLink="/products" >Products</a></li>
        <li><a routerLink="/about" >About Us</a></li>
        <li><a routerLink="/contact" >Contact Us</a></li>
    </ul>
</span>
<router-outlet></router-outlet>


Here the router-outlet is routing functionality that is used by the router to mark wherein a template, a matched component should be inserted.

Then inside the app-routing.module.ts file, we have provided these routes and let the angular know about these routes.

app-routing.module.ts




import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home.component'
import { ProductComponent } from './product.component'
import { AboutComponent } from './about.component'
import { ContactComponent } from './contact.component'
  
const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'products', component: ProductComponent },
  { path: 'about', component: AboutComponent },
  { path: 'contact', component: ContactComponent, },
];
  
@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule],
  providers: []
})
export class AppRoutingModule { }


And then simply import “AppRouting” module inside the app/module.ts file inside the @NgModule imports.

app.module.ts




import { NgModule } from '@angular/core';
import { HomeComponent } from './home.component'
import { ProductComponent } from './product.component'
import { AboutComponent } from './about.component'
import { ContactComponent } from './contact.component'
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
  
@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    ProductComponent,
    AboutComponent,
    ContactComponent
  ],
  imports: [
    AppRoutingModule,
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }


So now run this using “ng serve” in CLI and open localhost://4200 in the browser here you see your navigation bar, and you can navigate from one component to another without page reload.

Output:

BERJAYA



Similar Reads

What is Routing and Nested Routing in Angular 9/8 ?
In this article, we will learn the routing &amp; nested routing concept in Angular. We will implement the concept to establish routing between different components by making their routes when a user clicks the link, it will be navigated to a page link corresponding to the required component. Let's understand the routing in Angular. Routing: Angular
5 min read
Mastering React Routing: Learn Navigation and Routing in React Apps
In this article, we will learn how to implement Routing in React in an efficient way by implementing various aspects of routing such as dynamic routing, programmatically routing, and other concepts. Table of ContentWhat is Navigation in React?Routing with no page found(error 404) error handlingProgrammatic Navigation in ReactDynamic Routing with Re
6 min read
Routing in Angular JS using Angular UI Router
AngularJS is a front-end web application framework based on JavaScript and is maintained by Google. AngularJS interprets the attributes of HTML as directives to bind input/output components to a model represented by standard JavaScript variables. Pre-requisites: HTML CSS JavaScript AngularJS Angular-UI-Router is an AngularJS module used to create r
3 min read
How to use Angular MatTabsModule with routing in Angular 17?
The Angular Material library provides a tab component called MatTabsModule that allows you to organize content into separate tabs. When combined with Angular routing, you can create a seamless navigation experience where each tab loads its content from a different route. In this article, we'll explore how to use MatTabsModule with routing in an Ang
4 min read
How to enable routing and navigation between component pages in Angular 8 ?
The task is to enable routing between angular components by making their routes when a user clicks the link, it will be navigated to page link corresponding to the required component. Let us know what is routing in Angular Angular 8 routing: The Angular 8 Router helps to navigate between pages that are being triggered by the user's actions. The nav
2 min read
How to setup 404 page in angular routing ?
To set up a 404 page in the angular routing, we have to first create a component to display whenever a 404 error occurred. In the following approach, we will create a simple angular component called PagenotfoundComponent. Creating Component: Run the below command to create pagenotfound component. ng generate component pagenotfoundProject Structure:
2 min read
How to create module with Routing in Angular 9 ?
Angular applications are modular, and NgModules is Angular's own modular architecture. NgModules are containers for closely integrated application domains, workflows, or feature sets that comprise cohesive code blocks. Their scope is governed by the NgModule they include, and they can contain components, service providers, and other code files. You
4 min read
AngularJS Routing
Routing in AngularJS is used when the user wants to navigate to different pages in an application but still wants it to be a single-page application. AngularJS routes enable the user to create different URLs for different content in an application. The ngRoute module helps in accessing different pages of an application without reloading the entire
3 min read
Routing Path for ExpressJS
What and Why ? Routing in ExpressJS is used to subdivide and organize the web application into multiple mini-applications each having its own functionality. It provides more functionality by subdividing the web application rather than including all of the functionality on a single page. These mini-applications combine together to form a web applica
3 min read
Laravel | Routing Basics
Once you have installed Laravel and your basic Web App is up and running. Let's just look more deeply into the framework and see how we can work with routes. Routes: Routes are actually the web URLs that you can visit in your web application. For example /home, /profile, /dashboard etc are all different routes that one can create in a Laravel Appli
4 min read
three90RightbarBannerImg