Let’s
break down how to navigate through applications with routing in Angular.
πΊ️ What is Angular Routing?
Routing lets you build single-page
applications (SPAs) — where navigating to different "pages"
(views) doesn’t reload the browser, it just swaps out parts of the page.
✅ Example:
http://localhost:4200/home
http://localhost:4200/about
You can assign each
of these paths to a component, and Angular will render the right one
automatically.
⚙️ Step-by-Step: Set Up Routing in Angular
✅
1. Enable Routing in Your App
If you didn’t select
“Add Angular Routing” during project creation, make sure you have this in
app.module.ts:
app.module.ts
import {
RouterModule, Routes } from '@angular/router';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';
const routes: Routes
= [
{ path: 'home', component:
HomeComponent },
{ path: 'about', component:
AboutComponent },
{ path: '', redirectTo: 'home',
pathMatch: 'full' }, // default route
{ path: '**', redirectTo: 'home' }, //
wildcard (404)
];
@NgModule({
declarations: [AppComponent,
HomeComponent, AboutComponent],
imports: [BrowserModule,
RouterModule.forRoot(routes)],
bootstrap: [AppComponent]
})
export class AppModule {}
✅
2. Add <router-outlet> in Your Template
This is where Angular
will render the routed components.
app.component.html
<nav>
<a routerLink="/home"
routerLinkActive="active">Home</a>
<a routerLink="/about"
routerLinkActive="active">About</a>
</nav>
<router-outlet></router-outlet>
✅
3. Use routerLink to Navigate
In templates:
<a
routerLink="/about">Go to About</a>
In code (e.g., a
button click):
import { Router }
from '@angular/router';
constructor(private
router: Router) {}
goToAbout() {
this.router.navigate(['/about']);
}
π Advanced Routing Features
π Route Parameters
{ path: 'user/:id',
component: UserComponent }
In component:
constructor(private
route: ActivatedRoute) {}
ngOnInit() {
this.route.params.subscribe(params
=> {
console.log(params['id']);
});
}
π Route Guards
Protect routes from
unauthorized access:
{ path: 'admin',
component: AdminComponent, canActivate: [AuthGuard] }
π¦ Lazy Loading Modules
{ path: 'dashboard',
loadChildren: () => import('./dashboard/dashboard.module').then(m =>
m.DashboardModule) }
π§ Summary
|
Feature |
Purpose |
|
RouterModule |
Enables
routing |
|
routerLink |
Template
navigation |
|
router-outlet |
Displays
the current route’s component |
|
navigate() |
Navigate
in TypeScript |
|
ActivatedRoute |
Access
route params |
|
Route
Guards |
Protect
or control access |
Angular routing enables single-page applications to navigate between views without a full browser reload, with URL paths mapped to specific components. The article explains the basic routing setup, including defining routes, configuring RouterModule, and placing where routed components should be rendered.
ReplyDeleteThe routerLink and routerLinkActive examples show how navigation can be handled directly from Angular templates, while programmatic navigation uses the Router service. Developers working with Corporate Angular Training can use these patterns to build navigation between application views.
The article also introduces route parameters through ActivatedRoute, allowing components to access dynamic values such as a user ID from the URL. This provides a useful connection to Corporate TypeScript Training, where typed component and service code can work with routing data.
ReplyDeleteThe advanced routing section additionally introduces route guards for protecting restricted routes, alongside wildcard and default-route configurations. These concepts provide a practical foundation for Angular Projects that require structured navigation and controlled access to different application views.
ReplyDelete