Complete guide to routing in angular

Complete guide to routing in angular

In this article, we will explore Angular routing, why it is essential, and how to use it in Angular applications. Let's start by understanding what routing means in Angular.

What is routing in angular ?

Routing in Angular refers to the mechanism that allows users to navigate between different views or pages within an application without the need to reload the entire page. It enables the creation of single-page applications (SPAs) where the content dynamically changes based on the route or URL.

In simpler terms, imagine a website with multiple pages. When you click on a link, the entire page refreshes, and a new page loads. However, with Angular routing, only a specific portion of the page changes while the rest remains the same. This approach provides a smoother and faster user experience by eliminating the need to reload the entire page.

Angular routing works by defining routes in your application and associating each route with a specific component. When a user navigates to a particular URL or route, Angular matches the URL with the defined routes and loads the corresponding component, updating the view accordingly. This allows for different views or components to be displayed based on the routes within the application.

Overall, Angular routing simplifies navigation within an application, enhances the user experience, and facilitates the creation of dynamic and interactive single-page applications.

Now that we understand the concept and importance of Angular routing, let's learn how to use it in our applications.

Angular Routing in Practice:

To start, create an Angular application by running the following command:

ng new routing-project-demo

During the setup process, you will be prompted to choose whether to include routing. Select 'no' as we will manually install it in this article.

Ensure that you are in the desired directory for creating your application. You can replace 'routing-project-demo' with the name of your choice, or use this name if you prefer.

In our application, we will use three components: the default 'app' component that comes with the Angular project, the 'posts' component, and the 'users' component. Create these components using the following commands in the terminal:

ng generate component users

ng generate component posts

These commands will generate the 'users' and 'posts' components.

With the required components in place, let's configure the environment for routing.

In the 'app.module.ts' file, import the necessary modules for routing:

import { BrowserModule } from '@angular/platform-browser'; 
import { NgModule } from '@angular/core'; 
import { AppRoutingModule } from './app-routing.module'; 
import { AppComponent } from './app.component'; 
import { UsersComponent } from './users.component'; 
import { PostsComponent } from './posts.component';

@NgModule({
 declarations: [ AppComponent,
 UsersComponent,
 PostsComponent ],
 imports: [ BrowserModule,
 AppRoutingModule ],
 providers: [], 
bootstrap: [AppComponent] }) 

export class AppModule { }

In this code, we import 'AppRoutingModule' because it is used to define the routes. Now, let's see how to define the routes in the 'app-routing.module.ts' file:

import { NgModule } from '@angular/core'; 
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [
 { path: '', component: AppComponent }, 
{ path: 'users', component: UsersComponent }, 
{ path: 'posts', component: PostsComponent } ];

@NgModule({ 
imports: [RouterModule.forRoot(routes)], 
exports: [RouterModule] })

 export class AppRoutingModule { }

In this code, we import 'Routes' and 'RouterModule' from '@angular/router' to leverage Angular's routing capabilities. We then define our routes by specifying the path and the corresponding component for each route.

To ensure the routing works correctly, we need to declare these paths in our components. In the

'app.component.html' file, add the following code:

<!-- Heading for the application -->
<h1>Angular Routing Demo App</h1>

<!-- Navigation links -->
<nav>
  <!-- Link for the 'Users' route -->
  <button><a routerLink="/users">Users</a></button>

  <!-- Link for the 'Posts' route -->
  <button><a routerLink="/posts">Posts</a></button>
</nav>

<!-- Router outlet -->
<router-outlet></router-outlet>

Read the comments in the code for a better understanding of each part.

After implementing this code, clicking on 'Users' will redirect you to the 'UsersComponent', and clicking on 'Posts' will redirect you to the 'PostsComponent'.

By following this article, you should have grasped the basics of routing in Angular. For a more in-depth understanding, I highly recommend visiting the official documentation.

I hope you enjoyed this article. Don't forget to subscribe to this blog for more content like this.

Thank you for your time!

Happy coding!