Angular 2 – How to Configure Route Definitions – Part 1

This blog series would be used to describe different techniques which can be used to configure route definitions in Angular apps. The routing can be defined based on following three patterns:

  • Route definitions within AppModule
  • Routing defined as a separate module at app root level
  • Routing module defined within feature modules (recommended for enterprise apps)

In this blog, we will learn different aspects related with creating route definitions within AppModule. The most simple way of configuring route definitions is creating route definitions within AppModule file such as app.module.ts which is found at root level. This technique can be used for only learning purpose. When creating complex or enterprise apps, this is not the recommended way. The following is the what needs to be done to create route definitions within app.module.ts:

Import the Routing Libraries

The first step is to import the routing library such as RouterModule and Routes

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

Create Route Definitions

Create route definitions such as following in app.module.ts. Make sure to include route definitions for handling invalid routes using components such as PageNotFoundComponent and, default route to be used when app launches with empty path.

const appRoutes: Routes = [
    { path: 'register', component: UserRegistrationComponent, data: {title: 'New User Registration'} },
    { path: 'login', component: LoginComponent },
    { path: 'doctors', component: DoctorListComponent, data: { title: 'Doctors Information' } },
    { path: 'index', component: HomeComponent },
    { path: '', redirectTo: '/index', pathMatch: 'full' },
    { path: '**', component: PageNotFoundComponent }
];

Configure Router Module

Lastly, configure router module with RouterModule.forRoot as part of @NgModule meta definition.

@NgModule({
    imports: [..., RouterModule.forRoot(appRoutes),],
    declarations: [ AppComponent, ...],
    providers: [ ...],
    bootstrap: [ AppComponent ]
})
export class AppModule { }

Greater details in relation with above and much more can be obtained from my book, Building web apps with Spring 5 and Angular. Grab your ebook today and get started.

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. For latest updates and blogs, follow us on Twitter. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking. Check out my other blog, Revive-n-Thrive.com

Share
Published by
Ajitesh Kumar

Recent Posts

Mean Squared Error vs Cross Entropy Loss Function

Last updated: 28th April, 2024 As a data scientist, understanding the nuances of various cost…

3 days ago

Cross Entropy Loss Explained with Python Examples

Last updated: 28th April, 2024 In this post, you will learn the concepts related to…

3 days ago

Logistic Regression in Machine Learning: Python Example

Last updated: 26th April, 2024 In this blog post, we will discuss the logistic regression…

5 days ago

MSE vs RMSE vs MAE vs MAPE vs R-Squared: When to Use?

Last updated: 22nd April, 2024 As data scientists, we navigate a sea of metrics to…

6 days ago

Gradient Descent in Machine Learning: Python Examples

Last updated: 22nd April, 2024 This post will teach you about the gradient descent algorithm…

1 week ago

Loss Function vs Cost Function vs Objective Function: Examples

Last updated: 19th April, 2024 Among the terminologies used in training machine learning models, the…

2 weeks ago