Skip to content

Commit cb82a13

Browse files
committed
docs: Update README.md with additional Stackblitz examples for component communication and SPA section
1 parent 18e8038 commit cb82a13

File tree

1 file changed

+74
-90
lines changed

1 file changed

+74
-90
lines changed

README.md

+74-90
Original file line numberDiff line numberDiff line change
@@ -3688,11 +3688,8 @@ Routing is used to navigate between different components in an Angular applicati
36883688

36893689
```ts
36903690
import { NgModule } from '@angular/core';
3691-
36923691
import { RouterModule, Routes } from '@angular/router';
36933692

3694-
import { HomeComponent } from './home.component';
3695-
36963693
const routes: Routes = [];
36973694

36983695
@NgModule({
@@ -3703,19 +3700,24 @@ const routes: Routes = [];
37033700
export class AppRoutingModule {}
37043701
```
37053702

3706-
```ts
3707-
import { Component } from '@angular/core';
3708-
3709-
@Component({
3710-
selector: 'app-home',
3711-
template: '<h1>Home Component</h1>',
3712-
})
3703+
```typescript
3704+
import { Routes } from '@angular/router';
37133705

3714-
export class HomeComponent {}
3706+
export const routes: Routes = [];
37153707
```
37163708

3717-
```html
3718-
<router-outlet></router-outlet>
3709+
```typescript
3710+
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
3711+
import { provideRouter } from '@angular/router';
3712+
3713+
import { routes } from './app.routes';
3714+
3715+
export const appConfig: ApplicationConfig = {
3716+
providers: [
3717+
provideZoneChangeDetection({ eventCoalescing: true }),
3718+
provideRouter(routes),
3719+
],
3720+
};
37193721
```
37203722

37213723
[Back to top⤴️](#table-of-contents)
@@ -4079,6 +4081,8 @@ export class AppModule {}
40794081

40804082
Lazy loading is a technique used to load modules only when they are needed. This can help reduce the initial load time of the application by loading only the necessary modules.
40814083

4084+
Example of lazy loading in non standalone components:
4085+
40824086
```typescript
40834087
import { NgModule } from '@angular/core';
40844088
import { BrowserModule } from '@angular/platform-browser';
@@ -4190,12 +4194,68 @@ export class Page2Module {}
41904194
<router-outlet></router-outlet>
41914195
```
41924196

4193-
Certainly! Let's complete the Angular Router guide with examples for the provided sections:
4197+
Example of lazy loading in standalone components:
4198+
4199+
```typescript
4200+
import { Routes } from '@angular/router';
4201+
4202+
export const routes: Routes = [
4203+
{
4204+
path: 'page-1',
4205+
loadComponent: () => import('./page-1/page-1.component').then((m) => m.Page1Component),
4206+
},
4207+
{
4208+
path: 'page-2',
4209+
loadComponent: () => import('./page-2/page-2.component').then((m) => m.Page2Component),
4210+
}
4211+
];
4212+
```
4213+
4214+
```typescript
4215+
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
4216+
import { provideRouter } from '@angular/router';
4217+
4218+
import { routes } from './app.routes';
4219+
4220+
export const appConfig: ApplicationConfig = {
4221+
providers: [
4222+
provideZoneChangeDetection({ eventCoalescing: true }),
4223+
provideRouter(routes)
4224+
],
4225+
};
4226+
```
4227+
4228+
```typescript
4229+
import { Component } from '@angular/core';
4230+
import { RouterOutlet } from '@angular/router';
4231+
4232+
@Component({
4233+
selector: 'app-root',
4234+
standalone: true,
4235+
imports: [RouterOutlet],
4236+
templateUrl: './app.component.html',
4237+
styleUrl: './app.component.scss'
4238+
})
4239+
export class AppComponent {
4240+
// component logic here
4241+
}
4242+
```
4243+
4244+
```html
4245+
<h1>Lazy Loading Example</h1>
4246+
<a routerLink="page-1">Page-1</a> &nbsp;
4247+
<a routerLink="page-2">Page-2</a>
4248+
<router-outlet></router-outlet>
4249+
```
4250+
4251+
[Back to top⤴️](#table-of-contents)
41944252

41954253
### Router
41964254

41974255
The Angular Router is a powerful tool that allows you to define navigation paths and routes in your application. It enables you to navigate between different components and views based on the URL path.
41984256

4257+
Example of routing in Angular non standalone components:
4258+
41994259
```typescript
42004260
// app.module.ts
42014261
import { NgModule } from '@angular/core';
@@ -4294,82 +4354,6 @@ export class UserDetailsComponent {
42944354
}
42954355
```
42964356

4297-
## Routing Module
4298-
4299-
A routing module is a feature module that contains the routes and components related to a specific feature or section of an Angular application. It helps organize the application into smaller, more manageable modules.
4300-
4301-
```typescript
4302-
// app-routing.module.ts
4303-
import { NgModule } from '@angular/core';
4304-
import { RouterModule, Routes } from '@angular/router';
4305-
import { HomeComponent } from './home/home.component';
4306-
import { AboutComponent } from './about/about.component';
4307-
4308-
const routes: Routes = [
4309-
{
4310-
path: 'home',
4311-
component: HomeComponent
4312-
},
4313-
{
4314-
path: 'about',
4315-
component: AboutComponent
4316-
}
4317-
];
4318-
4319-
@NgModule({
4320-
imports: [RouterModule.forRoot(routes)],
4321-
exports: [RouterModule]
4322-
})
4323-
4324-
export class AppRoutingModule { }
4325-
```
4326-
4327-
```typescript
4328-
// app.module.ts
4329-
import { NgModule } from '@angular/core';
4330-
import { BrowserModule } from '@angular/platform-browser';
4331-
import { AppRoutingModule } from './app-routing.module';
4332-
import { AppComponent } from './app.component';
4333-
import { HomeComponent } from './home/home.component';
4334-
import { AboutComponent } from './about/about.component';
4335-
4336-
@NgModule({
4337-
declarations: [
4338-
AppComponent,
4339-
HomeComponent,
4340-
AboutComponent
4341-
],
4342-
imports: [
4343-
BrowserModule,
4344-
AppRoutingModule
4345-
],
4346-
providers: [],
4347-
bootstrap: [AppComponent]
4348-
})
4349-
4350-
export class AppModule { }
4351-
```
4352-
4353-
```html
4354-
<!-- app.component.html -->
4355-
<nav>
4356-
<a routerLink="/home">Home</a>
4357-
<a routerLink="/about">About</a>
4358-
</nav>
4359-
4360-
<router-outlet></router-outlet>
4361-
```
4362-
4363-
```html
4364-
<!-- home.component.html -->
4365-
<h1>Home Component</h1>
4366-
```
4367-
4368-
```html
4369-
<!-- about.component.html -->
4370-
<h1>About Component</h1>
4371-
```
4372-
43734357
## Route Parameters
43744358

43754359
Route parameters are used to pass data to a route in Angular. They allow you to create dynamic routes that can be customized based on user input or other factors. Route parameters are defined in the route configuration and can be accessed in the component associated with the route.

0 commit comments

Comments
 (0)