@@ -2060,36 +2060,108 @@ export class Page2Module {}
20602060< router- outlet>< / router- outlet>
20612061` ` `
20622062
2063- ## Router
2063+ Certainly! Let's complete the Angular Router guide with examples for the provided sections:
20642064
2065- Angular Router is a mechanism in which navigation happens from one view to the next as users perform application tasks. It borrows the concepts or model of browser's application navigation.
2065+ ### Router
2066+
2067+ Angular Router is a mechanism in which navigation happens from one view to the next as users perform application tasks. It borrows the concepts or model of a browser's application navigation.
20662068
20672069` ` ` typescript
2070+ // app.module.ts
2071+ import { NgModule } from ' @angular/core' ;
2072+ import { RouterModule , Routes } from ' @angular/router' ;
2073+
2074+ const routes: Routes = [
2075+ // Define your routes here
2076+ ];
20682077
2078+ @NgModule ({
2079+ imports: [RouterModule .forRoot (routes)],
2080+ exports: [RouterModule]
2081+ })
2082+ export class AppRoutingModule { }
20692083` ` `
20702084
2071- Required Route Params
2085+ ### Required Route Params
2086+
2087+ Route parameters are used to pass data to a route during navigation. In the example below, we have a route with a required parameter (` : id` ).
20722088
20732089` ` ` typescript
2090+ // app.module.ts
2091+ import { NgModule } from ' @angular/core' ;
2092+ import { RouterModule , Routes } from ' @angular/router' ;
2093+ import { UserDetailsComponent } from ' ./user-details/user-details.component' ;
20742094
2095+ const routes: Routes = [
2096+ { path: ' user/:id' , component: UserDetailsComponent },
2097+ // Other routes...
2098+ ];
2099+
2100+ @NgModule ({
2101+ imports: [RouterModule .forRoot (routes)],
2102+ exports: [RouterModule]
2103+ })
2104+ export class AppRoutingModule { }
20752105` ` `
20762106
2077- Navigating in app
2107+ ### Navigating in the App
2108+
2109+ To navigate to a route programmatically, you can use the ` Router` service. For example, in a component:
20782110
20792111` ` ` typescript
2112+ // some-component.component.ts
2113+ import { Router } from ' @angular/router' ;
2114+
2115+ export class SomeComponent {
2116+
2117+ constructor (private router : Router ) {}
20802118
2119+ navigateToUserDetails (userId: number): void {
2120+ this .router .navigate ([' /user' , userId]);
2121+ }
2122+ }
20812123` ` `
20822124
2083- Optional Route Params
2125+ ### Optional Route Params
2126+
2127+ You can make route parameters optional by appending a ` ? ` to them. In the example below, ` : id` is optional:
20842128
20852129` ` ` typescript
2130+ // app.module.ts
2131+ import { NgModule } from ' @angular/core' ;
2132+ import { RouterModule , Routes } from ' @angular/router' ;
2133+ import { UserDetailsComponent } from ' ./user-details/user-details.component' ;
2134+
2135+ const routes: Routes = [
2136+ { path: ' user/:id?' , component: UserDetailsComponent },
2137+ // Other routes...
2138+ ];
20862139
2140+ @NgModule ({
2141+ imports: [RouterModule .forRoot (routes)],
2142+ exports: [RouterModule]
2143+ })
2144+ export class AppRoutingModule { }
20872145` ` `
20882146
20892147### Params
20902148
2091- ` ` ` jsx
2149+ To access route parameters in a component, you can use the ` ActivatedRoute` service. For example, in ` user- details .component .ts ` :
2150+
2151+ ` ` ` typescript
2152+ // user-details.component.ts
2153+ import { ActivatedRoute } from ' @angular/router' ;
20922154
2155+ export class UserDetailsComponent {
2156+
2157+ constructor (private route : ActivatedRoute ) {
2158+ // Accessing route parameters
2159+ this .route .params .subscribe (params => {
2160+ const userId = params[' id' ];
2161+ // Do something with the userId...
2162+ });
2163+ }
2164+ }
20932165` ` `
20942166
20952167## Services & Dependency Injection
@@ -2433,55 +2505,132 @@ export class AuthGuard implements CanActivate {
24332505CanDeactivate
24342506
24352507` ` ` typescript
2508+ // can-deactivate.guard.ts
2509+ import { Injectable } from ' @angular/core' ;
2510+ import { CanDeactivate } from ' @angular/router' ;
2511+ import { Observable } from ' rxjs' ;
24362512
2437- ` ` `
2438-
2439- Resolve
2513+ export interface CanDeactivateComponent {
2514+ canDeactivate : () => boolean | Observable < boolean >;
2515+ }
24402516
2441- ` ` ` typescript
2517+ @Injectable ({
2518+ providedIn: ' root'
2519+ })
2520+ export class CanDeactivateGuard
2521+ implements CanDeactivate< CanDeactivateComponent> {
24422522
2523+ canDeactivate (
2524+ component: CanDeactivateComponent
2525+ ): boolean | Observable< boolean> {
2526+ return component .canDeactivate ? component .canDeactivate () : true ;
2527+ }
2528+ }
24432529` ` `
24442530
2445- CanLoad
2446-
24472531` ` ` typescript
2532+ // your-component.component.ts
2533+ import { Component } from ' @angular/core' ;
2534+ import { CanDeactivateComponent } from ' ./can-deactivate.guard' ;
24482535
2449- ` ` `
2450-
2451- CanActivateChild
2452-
2453- ` ` ` typescript
2536+ @Component ({
2537+ selector: ' app-your-component' ,
2538+ template: ' Your component content here.'
2539+ })
2540+ export class YourComponent implements CanDeactivateComponent {
2541+ // ... your component code ...
24542542
2543+ canDeactivate (): boolean | Observable< boolean> {
2544+ // Your deactivation logic goes here
2545+ // Return true if navigation is allowed, false otherwise
2546+ return true ;
2547+ }
2548+ }
24552549` ` `
24562550
2457- API/Server Calls and CORS
2551+ Resolve
24582552
24592553` ` ` typescript
2554+ // resolve.guard.ts
2555+ import { Injectable } from ' @angular/core' ;
2556+ import { Resolve , ActivatedRouteSnapshot , RouterStateSnapshot } from ' @angular/router' ;
2557+ import { Observable } from ' rxjs' ;
24602558
2559+ @Injectable ({
2560+ providedIn: ' root'
2561+ })
2562+ export class DataResolver implements Resolve< string> {
2563+
2564+ resolve (
2565+ route: ActivatedRouteSnapshot,
2566+ state: RouterStateSnapshot
2567+ ): string | Observable< string> | Promise < string> {
2568+ // Fetch data asynchronously and return it
2569+ return ' Data resolved asynchronously' ;
2570+ }
2571+ }
24612572` ` `
24622573
2463- Different Environments
2574+ CanLoad
24642575
24652576` ` ` typescript
2577+ // can-load.guard.ts
2578+ import { Injectable } from ' @angular/core' ;
2579+ import { CanLoad , Route , UrlSegment , UrlTree } from ' @angular/router' ;
2580+ import { Observable } from ' rxjs' ;
24662581
2467- ` ` `
2468-
2469- Caching
2582+ @Injectable ({
2583+ providedIn: ' root'
2584+ })
2585+ export class CanLoadGuard implements CanLoad {
24702586
2471- ` ` ` typescript
2587+ canLoad (
2588+ route: Route,
2589+ segments: UrlSegment[]
2590+ ): boolean | UrlTree | Observable< boolean | UrlTree> | Promise < boolean | UrlTree> {
2591+ // Check if the user is authenticated to load the module lazily
2592+ const isAuthenticated = /* your authentication logic here */ ;
24722593
2594+ if (isAuthenticated) {
2595+ return true ; // Allow lazy loading
2596+ } else {
2597+ // Redirect to the login page if not authenticated
2598+ return false ;
2599+ }
2600+ }
2601+ }
24732602` ` `
24742603
2475- Production Build
2604+ CanActivateChild
24762605
24772606` ` ` typescript
2607+ // auth-child.guard.ts
2608+ import { Injectable } from ' @angular/core' ;
2609+ import { CanActivateChild , ActivatedRouteSnapshot , RouterStateSnapshot , UrlTree , Router } from ' @angular/router' ;
2610+ import { Observable } from ' rxjs' ;
24782611
2479- ` ` `
2612+ @Injectable ({
2613+ providedIn: ' root'
2614+ })
2615+ export class AuthChildGuard implements CanActivateChild {
24802616
2481- Base Href
2617+ constructor ( private router : Router ) {}
24822618
2483- ` ` ` typescript
2619+ canActivateChild (
2620+ route: ActivatedRouteSnapshot,
2621+ state: RouterStateSnapshot
2622+ ): boolean | UrlTree | Observable< boolean | UrlTree> | Promise < boolean | UrlTree> {
2623+ // Check if the user is authenticated for child routes
2624+ const isAuthenticated = /* your authentication logic here */ ;
24842625
2626+ if (isAuthenticated) {
2627+ return true ; // Allow navigation to child routes
2628+ } else {
2629+ // Redirect to the login page if not authenticated
2630+ return this .router .createUrlTree ([' /login' ]);
2631+ }
2632+ }
2633+ }
24852634` ` `
24862635
24872636**ng-container** - A special element that can hold structural directives without adding new elements to the DOM.
0 commit comments