@aliaksei-raketski/pi-angular-developer 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -0
- package/overlays/angular-developer/references/docs-helpers.md +36 -0
- package/overlays/angular-developer/scripts/get-best-practices.mjs +268 -0
- package/overlays/angular-developer/scripts/search-documentation.mjs +396 -0
- package/package.json +41 -0
- package/scripts/sync-angular-skill.mjs +307 -0
- package/skills/angular-developer/SKILL.md +133 -0
- package/skills/angular-developer/UPSTREAM.md +9 -0
- package/skills/angular-developer/data/best-practices.md +56 -0
- package/skills/angular-developer/references/angular-animations.md +160 -0
- package/skills/angular-developer/references/angular-aria.md +597 -0
- package/skills/angular-developer/references/cli.md +86 -0
- package/skills/angular-developer/references/component-harnesses.md +57 -0
- package/skills/angular-developer/references/component-styling.md +91 -0
- package/skills/angular-developer/references/components.md +117 -0
- package/skills/angular-developer/references/creating-services.md +97 -0
- package/skills/angular-developer/references/data-resolvers.md +69 -0
- package/skills/angular-developer/references/define-routes.md +67 -0
- package/skills/angular-developer/references/defining-providers.md +72 -0
- package/skills/angular-developer/references/di-fundamentals.md +118 -0
- package/skills/angular-developer/references/docs-helpers.md +36 -0
- package/skills/angular-developer/references/e2e-testing.md +66 -0
- package/skills/angular-developer/references/effects.md +83 -0
- package/skills/angular-developer/references/environment-configuration.md +132 -0
- package/skills/angular-developer/references/hierarchical-injectors.md +43 -0
- package/skills/angular-developer/references/host-elements.md +80 -0
- package/skills/angular-developer/references/injection-context.md +63 -0
- package/skills/angular-developer/references/inputs.md +101 -0
- package/skills/angular-developer/references/linked-signal.md +59 -0
- package/skills/angular-developer/references/loading-strategies.md +61 -0
- package/skills/angular-developer/references/migrations.md +30 -0
- package/skills/angular-developer/references/navigate-to-routes.md +69 -0
- package/skills/angular-developer/references/outputs.md +86 -0
- package/skills/angular-developer/references/reactive-forms.md +118 -0
- package/skills/angular-developer/references/rendering-strategies.md +44 -0
- package/skills/angular-developer/references/resource.md +74 -0
- package/skills/angular-developer/references/route-animations.md +56 -0
- package/skills/angular-developer/references/route-guards.md +52 -0
- package/skills/angular-developer/references/router-lifecycle.md +45 -0
- package/skills/angular-developer/references/router-testing.md +87 -0
- package/skills/angular-developer/references/show-routes-with-outlets.md +68 -0
- package/skills/angular-developer/references/signal-forms.md +907 -0
- package/skills/angular-developer/references/signals-overview.md +94 -0
- package/skills/angular-developer/references/tailwind-css.md +69 -0
- package/skills/angular-developer/references/template-driven-forms.md +114 -0
- package/skills/angular-developer/references/testing-fundamentals.md +63 -0
- package/skills/angular-developer/scripts/get-best-practices.mjs +268 -0
- package/skills/angular-developer/scripts/search-documentation.mjs +396 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# Route Loading Strategies
|
|
2
|
+
|
|
3
|
+
Angular supports two main strategies for loading routes and components to balance initial load time and navigation responsiveness.
|
|
4
|
+
|
|
5
|
+
## Eager Loading
|
|
6
|
+
|
|
7
|
+
Components are bundled into the initial JavaScript payload and are available immediately.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
{ path: 'home', component: Home }
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
- **Pros**: Seamless transitions.
|
|
14
|
+
- **Cons**: Increases initial bundle size.
|
|
15
|
+
|
|
16
|
+
## Lazy Loading
|
|
17
|
+
|
|
18
|
+
Components or routes are loaded only when the user navigates to them. This creates separate JavaScript "chunks".
|
|
19
|
+
|
|
20
|
+
### Lazy Loading Components
|
|
21
|
+
|
|
22
|
+
Use `loadComponent` to fetch the component on demand.
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
{
|
|
26
|
+
path: 'admin',
|
|
27
|
+
loadComponent: () => import('./admin/admin.component').then(m => m.AdminComponent)`,
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Lazy Loading Child Routes
|
|
32
|
+
|
|
33
|
+
Use `loadChildren` to fetch a set of routes.
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
{
|
|
37
|
+
path: 'settings',
|
|
38
|
+
loadChildren: () => import('./settings/settings.routes'),
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Injection Context and Lazy Loading
|
|
43
|
+
|
|
44
|
+
Loader functions run within the **injection context** of the current route. This allows you to call `inject()` to make context-aware loading decisions.
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
{
|
|
48
|
+
path: 'dashboard',
|
|
49
|
+
loadComponent: () => {
|
|
50
|
+
const flags = inject(FeatureFlags);
|
|
51
|
+
return flags.isPremium
|
|
52
|
+
? import('./premium-dashboard')
|
|
53
|
+
: import('./basic-dashboard');
|
|
54
|
+
},
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Recommendation
|
|
59
|
+
|
|
60
|
+
- Use **Eager Loading** for the primary landing pages.
|
|
61
|
+
- Use **Lazy Loading** for all other feature areas to keep the initial bundle small.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# Automatic Migrations & Code Modernization
|
|
2
|
+
|
|
3
|
+
When tasked with refactoring or modernizing an existing codebase, always prefer using the official automated schematics available in `@angular/core` over manual text replacement.
|
|
4
|
+
|
|
5
|
+
## Discovering Migrations
|
|
6
|
+
|
|
7
|
+
To view all available schematics for the installed version of the core framework, run:
|
|
8
|
+
`ng generate @angular/core: --help`
|
|
9
|
+
|
|
10
|
+
## Common Migration Schematics
|
|
11
|
+
|
|
12
|
+
Use the following commands to apply specific syntax updates. You can scope these commands to a specific project or directory using the `--project <name>` or `--path <dir>` flags.
|
|
13
|
+
|
|
14
|
+
| Feature to Modernize | Command to Execute |
|
|
15
|
+
| :------------------------ | :---------------------------------------------------------- |
|
|
16
|
+
| **Built-in Control Flow** | `ng generate @angular/core:control-flow` |
|
|
17
|
+
| **Signal-based Inputs** | `ng generate @angular/core:signal-input-migration` |
|
|
18
|
+
| **Signal Queries** | `ng generate @angular/core:signal-queries-migration` |
|
|
19
|
+
| **Functional Outputs** | `ng generate @angular/core:output-migration` |
|
|
20
|
+
| **`inject()` Function** | `ng generate @angular/core:inject` |
|
|
21
|
+
| **Self-Closing Tags** | `ng generate @angular/core:self-closing-tag` |
|
|
22
|
+
| **Standalone** | `ng generate @angular/core:standalone` (See workflow below) |
|
|
23
|
+
|
|
24
|
+
## Specialized Workflow: Migrating to Standalone
|
|
25
|
+
|
|
26
|
+
The Standalone migration is an interactive, multi-step refactoring. You **MUST** perform this in three discrete stages, verifying that the application builds and runs correctly after each stage completes:
|
|
27
|
+
|
|
28
|
+
1. **Phase 1**: Run `ng generate @angular/core:standalone` and select the option to **Convert all components, directives, and pipes to standalone**.
|
|
29
|
+
2. **Phase 2**: Verify the build with `ng build`. Run the command again and select **Remove unnecessary NgModule classes**.
|
|
30
|
+
3. **Phase 3**: Verify the build with `ng build`. Run the final pass and select **Bootstrap the project using standalone APIs**.
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# Navigate to Routes
|
|
2
|
+
|
|
3
|
+
Angular provides both declarative and programmatic ways to navigate between routes.
|
|
4
|
+
|
|
5
|
+
## Declarative Navigation (`RouterLink`)
|
|
6
|
+
|
|
7
|
+
Use the `RouterLink` directive on anchor elements.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import {RouterLink, RouterLinkActive} from '@angular/router';
|
|
11
|
+
|
|
12
|
+
@Component({
|
|
13
|
+
imports: [RouterLink, RouterLinkActive],
|
|
14
|
+
template: `
|
|
15
|
+
<nav>
|
|
16
|
+
<a routerLink="/dashboard" routerLinkActive="active-link">Dashboard</a>
|
|
17
|
+
<a [routerLink]="['/user', userId]">Profile</a>
|
|
18
|
+
</nav>
|
|
19
|
+
`,
|
|
20
|
+
})
|
|
21
|
+
export class Nav {
|
|
22
|
+
userId = '123';
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
- **Absolute Paths**: Start with `/` (e.g., `/settings`).
|
|
27
|
+
- **Relative Paths**: No leading `/`. Use `../` to go up a level.
|
|
28
|
+
|
|
29
|
+
## Programmatic Navigation (`Router`)
|
|
30
|
+
|
|
31
|
+
Inject the `Router` service to navigate via TypeScript code.
|
|
32
|
+
|
|
33
|
+
### `router.navigate()`
|
|
34
|
+
|
|
35
|
+
Uses an array of commands.
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
private router = inject(Router);
|
|
39
|
+
private route = inject(ActivatedRoute);
|
|
40
|
+
|
|
41
|
+
// Standard navigation
|
|
42
|
+
this.router.navigate(['/profile']);
|
|
43
|
+
|
|
44
|
+
// With parameters
|
|
45
|
+
this.router.navigate(['/search'], {
|
|
46
|
+
queryParams: { q: 'angular' },
|
|
47
|
+
fragment: 'results'
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
// Relative navigation
|
|
51
|
+
this.router.navigate(['edit'], { relativeTo: this.route });
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### `router.navigateByUrl()`
|
|
55
|
+
|
|
56
|
+
Uses a string path. Ideal for absolute navigation or full URLs.
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
this.router.navigateByUrl('/products/123?view=details');
|
|
60
|
+
|
|
61
|
+
// Replace current entry in history
|
|
62
|
+
this.router.navigateByUrl('/login', {replaceUrl: true});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## URL Parameters
|
|
66
|
+
|
|
67
|
+
- **Route Params**: Part of the path (e.g., `/user/123`).
|
|
68
|
+
- **Query Params**: After the `?` (e.g., `/search?q=query`).
|
|
69
|
+
- **Matrix Params**: Scoped to a segment (e.g., `/products;category=books`).
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# Outputs (Custom Events)
|
|
2
|
+
|
|
3
|
+
Outputs allow a child component to emit custom events that a parent component can listen to. Angular recommends using the new `output()` function for modern applications.
|
|
4
|
+
|
|
5
|
+
## Function-based outputs
|
|
6
|
+
|
|
7
|
+
Declare outputs using the `output()` function. This returns an `OutputEmitterRef`.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import {Component, output} from '@angular/core';
|
|
11
|
+
|
|
12
|
+
@Component({
|
|
13
|
+
selector: 'custom-slider',
|
|
14
|
+
template: `<button (click)="changeValue(50)">Set to 50</button>`,
|
|
15
|
+
})
|
|
16
|
+
export class CustomSlider {
|
|
17
|
+
// Output without event data
|
|
18
|
+
readonly panelClosed = output<void>();
|
|
19
|
+
|
|
20
|
+
// Output with event data (number)
|
|
21
|
+
readonly valueChanged = output<number>();
|
|
22
|
+
|
|
23
|
+
changeValue(newValue: number) {
|
|
24
|
+
this.valueChanged.emit(newValue);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Usage in Template
|
|
30
|
+
|
|
31
|
+
Bind to the output event using parentheses `()`. If the event emits data, access it using the special `$event` variable.
|
|
32
|
+
|
|
33
|
+
```html
|
|
34
|
+
<custom-slider (panelClosed)="savePanelState()" (valueChanged)="logValue($event)" />
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Configuration Options
|
|
38
|
+
|
|
39
|
+
The `output` function accepts a config object to specify an alias.
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
@Component({...})
|
|
43
|
+
export class CustomSlider {
|
|
44
|
+
// The event is named 'valueChanged' in the template,
|
|
45
|
+
// but accessed as 'changed' in the component class.
|
|
46
|
+
readonly changed = output<number>({ alias: 'valueChanged' });
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Programmatic Subscription
|
|
51
|
+
|
|
52
|
+
When creating components dynamically, you can subscribe to outputs programmatically:
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
const componentRef = viewContainerRef.createComponent(CustomSlider);
|
|
56
|
+
|
|
57
|
+
const subscription = componentRef.instance.valueChanged.subscribe((val) => {
|
|
58
|
+
console.log('Value changed:', val);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// Clean up manually if needed (Angular cleans up destroyed components automatically)
|
|
62
|
+
subscription.unsubscribe();
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Decorator-based Outputs (@Output)
|
|
66
|
+
|
|
67
|
+
The legacy API uses the `@Output()` decorator with an `EventEmitter`. It remains supported but is not recommended for new code.
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
import { Component, Output, EventEmitter } from '@angular/core';
|
|
71
|
+
|
|
72
|
+
@Component({...})
|
|
73
|
+
export class LegacyExample {
|
|
74
|
+
@Output() readonly valueChanged = new EventEmitter<number>();
|
|
75
|
+
|
|
76
|
+
// With alias
|
|
77
|
+
@Output('customEventName') readonly changed = new EventEmitter<void>();
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Best Practices
|
|
82
|
+
|
|
83
|
+
- **Prefer `output()`**: Use the function-based `output()` instead of `@Output()` and `EventEmitter`.
|
|
84
|
+
- **Naming**: Use `camelCase` for output names. Avoid prefixing with `on` (e.g., use `valueChanged` instead of `onValueChanged`).
|
|
85
|
+
- **No DOM Bubbling**: Angular custom events do not bubble up the DOM tree like native events.
|
|
86
|
+
- **Avoid Collisions**: Do not choose names that collide with native DOM events (like `click` or `submit`).
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# Reactive Forms
|
|
2
|
+
|
|
3
|
+
Reactive forms provide a model-driven approach to handling form inputs. They are built around observable streams and provide synchronous access to the data model, making them more scalable and testable than template-driven forms.
|
|
4
|
+
|
|
5
|
+
## Core Classes
|
|
6
|
+
|
|
7
|
+
Reactive forms are built using these fundamental classes from `@angular/forms`:
|
|
8
|
+
|
|
9
|
+
- `FormControl`: Manages the value and validity of an individual input.
|
|
10
|
+
- `FormGroup`: Manages a group of controls (an object-like structure).
|
|
11
|
+
- `FormArray`: Manages a numerically indexed array of controls.
|
|
12
|
+
- `FormBuilder`/`NonNullableFormBuilder`: A service that provides factory methods for creating control instances.
|
|
13
|
+
|
|
14
|
+
## Setup
|
|
15
|
+
|
|
16
|
+
Import `ReactiveFormsModule` into your component.
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import {Component, inject} from '@angular/core';
|
|
20
|
+
import {ReactiveFormsModule, NonNullableFormBuilder, Validators} from '@angular/forms';
|
|
21
|
+
|
|
22
|
+
@Component({
|
|
23
|
+
selector: 'app-profile-editor',
|
|
24
|
+
imports: [ReactiveFormsModule],
|
|
25
|
+
templateUrl: './profile-editor.component.html',
|
|
26
|
+
})
|
|
27
|
+
export class ProfileEditor {
|
|
28
|
+
private readonly fb = inject(NonNullableFormBuilder);
|
|
29
|
+
|
|
30
|
+
// Using FormBuilder for concise definition
|
|
31
|
+
protected readonly profileForm = this.fb.group({
|
|
32
|
+
firstName: ['', Validators.required],
|
|
33
|
+
lastName: '',
|
|
34
|
+
address: this.fb.group({
|
|
35
|
+
street: '',
|
|
36
|
+
city: '',
|
|
37
|
+
}),
|
|
38
|
+
aliases: this.fb.array([this.fb.control('')]),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
protected onSubmit() {
|
|
42
|
+
console.warn(this.profileForm.value);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Template Binding
|
|
48
|
+
|
|
49
|
+
Use directives to bind the model to the view:
|
|
50
|
+
|
|
51
|
+
- `[formGroup]`: Binds a `FormGroup` to a `<form>` or `<div>`.
|
|
52
|
+
- `formControlName`: Binds a named control within a group to an input.
|
|
53
|
+
- `formGroupName`: Binds a nested `FormGroup`.
|
|
54
|
+
- `formArrayName`: Binds a nested `FormArray`.
|
|
55
|
+
- `[formControl]`: Binds a standalone `FormControl`.
|
|
56
|
+
|
|
57
|
+
```html
|
|
58
|
+
<form [formGroup]="profileForm" (ngSubmit)="onSubmit()">
|
|
59
|
+
<input type="text" formControlName="firstName" />
|
|
60
|
+
|
|
61
|
+
<div formGroupName="address">
|
|
62
|
+
<input type="text" formControlName="street" />
|
|
63
|
+
</div>
|
|
64
|
+
|
|
65
|
+
<div formArrayName="aliases">
|
|
66
|
+
@for (alias of profileForm.controls.aliases.controls; track alias) {
|
|
67
|
+
<input type="text" [formControlName]="$index" />
|
|
68
|
+
}
|
|
69
|
+
</div>
|
|
70
|
+
|
|
71
|
+
<button type="submit" [disabled]="!profileForm.valid">Submit</button>
|
|
72
|
+
</form>
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Accessing Controls
|
|
76
|
+
|
|
77
|
+
Use `.controls` for easy access to controls.
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
addAlias() {
|
|
81
|
+
this.profileForm.controls.aliases.push(this.fb.control(''));
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Updating Values
|
|
86
|
+
|
|
87
|
+
- `patchValue()`: Updates only the specified properties. Fails silently on structural mismatches.
|
|
88
|
+
- `setValue()`: Replaces the entire model. Strictly enforces the form structure.
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
updateProfile() {
|
|
92
|
+
this.profileForm.patchValue({
|
|
93
|
+
firstName: 'Nancy',
|
|
94
|
+
address: { street: '123 Drew Street' }
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Unified Change Events
|
|
100
|
+
|
|
101
|
+
Modern Angular (v18+) provides a single `events` observable on all controls to track value, status, pristine, touched, reset, and submit events.
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
import {ValueChangeEvent, StatusChangeEvent} from '@angular/forms';
|
|
105
|
+
|
|
106
|
+
this.profileForm.events.subscribe((event) => {
|
|
107
|
+
if (event instanceof ValueChangeEvent) {
|
|
108
|
+
console.log('New value:', event.value);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Manual State Management
|
|
114
|
+
|
|
115
|
+
- `markAsTouched()` / `markAllAsTouched()`: Useful for showing validation errors on submit.
|
|
116
|
+
- `markAsDirty()` / `markAsPristine()`: Tracks if the value has been modified.
|
|
117
|
+
- `updateValueAndValidity()`: Manually triggers recalculation of value and status.
|
|
118
|
+
- Options `{ emitEvent: false }` or `{ onlySelf: true }` can be passed to most methods to control propagation.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Rendering Strategies
|
|
2
|
+
|
|
3
|
+
Angular supports multiple rendering strategies to optimize for SEO, performance, and interactivity.
|
|
4
|
+
|
|
5
|
+
## 1. Client-Side Rendering (CSR)
|
|
6
|
+
|
|
7
|
+
**Default Strategy.** Content is rendered entirely in the browser.
|
|
8
|
+
|
|
9
|
+
- **Use case**: Interactive dashboards, internal tools.
|
|
10
|
+
- **Pros**: Simplest to configure, low server cost.
|
|
11
|
+
- **Cons**: Poor SEO, slower initial content visibility (must wait for JS).
|
|
12
|
+
|
|
13
|
+
## 2. Static Site Generation (SSG / Prerendering)
|
|
14
|
+
|
|
15
|
+
Content is pre-rendered into static HTML files at **build time**.
|
|
16
|
+
|
|
17
|
+
- **Use case**: Marketing pages, blogs, documentation.
|
|
18
|
+
- **Pros**: Fastest initial load, excellent SEO, CDN-friendly.
|
|
19
|
+
- **Cons**: Requires rebuild for content updates, not for user-specific data.
|
|
20
|
+
|
|
21
|
+
## 3. Server-Side Rendering (SSR)
|
|
22
|
+
|
|
23
|
+
Content is rendered on the server for the **initial request**. Subsequent navigations happen client-side (SPA style).
|
|
24
|
+
|
|
25
|
+
- **Use case**: E-commerce product pages, news sites, personalized dynamic content.
|
|
26
|
+
- **Pros**: Excellent SEO, fast initial content visibility.
|
|
27
|
+
- **Cons**: Requires a server (Node.js), higher server cost/latency.
|
|
28
|
+
|
|
29
|
+
## Hydration
|
|
30
|
+
|
|
31
|
+
Hydration is the process of making server-rendered HTML interactive in the browser.
|
|
32
|
+
|
|
33
|
+
- **Full Hydration**: The entire app becomes interactive at once.
|
|
34
|
+
- **Incremental Hydration**: (Advanced) Parts become interactive as needed using `@defer` blocks.
|
|
35
|
+
- **Event Replay**: Captures and replays user events that happened before hydration finished.
|
|
36
|
+
|
|
37
|
+
## Decision Matrix
|
|
38
|
+
|
|
39
|
+
| Requirement | Strategy |
|
|
40
|
+
| :------------------------------ | :------------------- |
|
|
41
|
+
| **SEO + Static Content** | SSG |
|
|
42
|
+
| **SEO + Dynamic Content** | SSR |
|
|
43
|
+
| **No SEO + High Interactivity** | CSR |
|
|
44
|
+
| **Mixed** | Hybrid (Route-based) |
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Async Reactivity with `resource`
|
|
2
|
+
|
|
3
|
+
A `Resource` incorporates asynchronous data fetching into Angular's signal-based reactivity. It executes an async loader function whenever its dependencies change, exposing the status and result as synchronous signals.
|
|
4
|
+
|
|
5
|
+
## Basic Usage
|
|
6
|
+
|
|
7
|
+
The `resource` function accepts an options object with two main properties:
|
|
8
|
+
|
|
9
|
+
1. `params`: A reactive computation (like `computed`). When signals read here change, the resource re-fetches.
|
|
10
|
+
2. `loader`: An async function that fetches data based on the parameters.
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { Component, resource, signal, computed } from '@angular/core';
|
|
14
|
+
|
|
15
|
+
@Component({...})
|
|
16
|
+
export class UserProfile {
|
|
17
|
+
protected readonly userId = signal('123');
|
|
18
|
+
|
|
19
|
+
protected readonly userResource = resource({
|
|
20
|
+
// Reactively tracking userId
|
|
21
|
+
params: () => ({ id: this.userId() }),
|
|
22
|
+
|
|
23
|
+
// Executes whenever params change
|
|
24
|
+
loader: async ({ params, abortSignal }) => {
|
|
25
|
+
const response = await fetch(`/api/users/${params.id}`, { signal: abortSignal });
|
|
26
|
+
if (!response.ok) throw new Error('Network error');
|
|
27
|
+
return response.json();
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// Use the resource value in computed signals
|
|
32
|
+
protected readonly userName = computed(() => {
|
|
33
|
+
if (this.userResource.hasValue()) {
|
|
34
|
+
return this.userResource.value()?.name;
|
|
35
|
+
} else {
|
|
36
|
+
return 'Loading...';
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Aborting Requests
|
|
43
|
+
|
|
44
|
+
If the `params` signal changes while a previous loader is still running, the `Resource` will attempt to abort the outstanding request using the provided `abortSignal`. **Always pass `abortSignal` to your `fetch` calls.**
|
|
45
|
+
|
|
46
|
+
## Reloading Data
|
|
47
|
+
|
|
48
|
+
You can imperatively force the resource to re-run the loader without the params changing by calling `.reload()`.
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
this.userResource.reload();
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Resource Status Signals
|
|
55
|
+
|
|
56
|
+
The `Resource` object provides several signals to read its current state:
|
|
57
|
+
|
|
58
|
+
- `value()`: The resolved data, or `undefined`.
|
|
59
|
+
- `hasValue()`: Type-guard boolean. `true` if a value exists.
|
|
60
|
+
- `isLoading()`: Boolean indicating if the loader is currently running.
|
|
61
|
+
- `error()`: The error thrown by the loader, or `undefined`.
|
|
62
|
+
- `status()`: A string constant representing the exact state (`'idle'`, `'loading'`, `'resolved'`, `'error'`, `'reloading'`, `'local'`).
|
|
63
|
+
|
|
64
|
+
## Local Mutation
|
|
65
|
+
|
|
66
|
+
You can optimistically update the resource's value directly. This changes the status to `'local'`.
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
this.userResource.value.set({name: 'Optimistic Update'});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Reactive Data Fetching with `httpResource`
|
|
73
|
+
|
|
74
|
+
If you are using Angular's `HttpClient`, prefer using `httpResource`. It is a specialized wrapper that leverages the Angular HTTP stack (including interceptors) while providing the same signal-based resource API.
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# Route Transition Animations
|
|
2
|
+
|
|
3
|
+
Angular Router supports the browser's **View Transitions API** for smooth visual transitions between routes.
|
|
4
|
+
|
|
5
|
+
## Enabling View Transitions
|
|
6
|
+
|
|
7
|
+
Add `withViewTransitions()` to your router configuration.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
provideRouter(routes, withViewTransitions());
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
This is a **progressive enhancement**. In browsers that don't support the API, the router will still work but without the transition animation.
|
|
14
|
+
|
|
15
|
+
## How it Works
|
|
16
|
+
|
|
17
|
+
1. Browser takes a screenshot of the old state.
|
|
18
|
+
2. Router updates the DOM (activates new component).
|
|
19
|
+
3. Browser takes a screenshot of the new state.
|
|
20
|
+
4. Browser animates between the two states.
|
|
21
|
+
|
|
22
|
+
## Customizing with CSS
|
|
23
|
+
|
|
24
|
+
Transitions are customized in **global CSS files** (not component-scoped CSS).
|
|
25
|
+
|
|
26
|
+
Use the `::view-transition-old()` and `::view-transition-new()` pseudo-elements.
|
|
27
|
+
|
|
28
|
+
```css
|
|
29
|
+
/* Example: Cross-fade + Slide */
|
|
30
|
+
::view-transition-old(root) {
|
|
31
|
+
animation: 90ms cubic-bezier(0.4, 0, 1, 1) both fade-out;
|
|
32
|
+
}
|
|
33
|
+
::view-transition-new(root) {
|
|
34
|
+
animation: 210ms cubic-bezier(0, 0, 0.2, 1) 90ms both fade-in;
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Advanced Control
|
|
39
|
+
|
|
40
|
+
Use `onViewTransitionCreated` to skip transitions or customize behavior based on the navigation context.
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
withViewTransitions({
|
|
44
|
+
onViewTransitionCreated: ({transition, from, to}) => {
|
|
45
|
+
// Skip animation for specific routes
|
|
46
|
+
if (to.url === '/no-animation') {
|
|
47
|
+
transition.skipTransition();
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Best Practices
|
|
54
|
+
|
|
55
|
+
- **Global Styles**: Always define transition animations in `styles.css` to avoid view encapsulation issues.
|
|
56
|
+
- **View Transition Names**: Assign unique `view-transition-name` to elements that should transition smoothly across routes (e.g., a header image).
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# Route Guards
|
|
2
|
+
|
|
3
|
+
Route guards control whether a user can navigate to or leave a route.
|
|
4
|
+
|
|
5
|
+
## Types of Guards
|
|
6
|
+
|
|
7
|
+
- **`CanActivate`**: Can the user access this route? (e.g., Auth check).
|
|
8
|
+
- **`CanActivateChild`**: Can the user access children of this route?
|
|
9
|
+
- **`CanDeactivate`**: Can the user leave this route? (e.g., Unsaved changes).
|
|
10
|
+
- **`CanMatch`**: Should this route even be considered for matching? (e.g., Feature flags). If it returns `false`, the router continues checking other routes.
|
|
11
|
+
|
|
12
|
+
## Creating a Guard
|
|
13
|
+
|
|
14
|
+
Guards are typically functional since Angular 15.
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
export const authGuard: CanActivateFn = (route, state) => {
|
|
18
|
+
const authService = inject(AuthService);
|
|
19
|
+
const router = inject(Router);
|
|
20
|
+
|
|
21
|
+
if (authService.isLoggedIn()) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Redirect to login
|
|
26
|
+
return router.parseUrl('/login');
|
|
27
|
+
};
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Applying Guards
|
|
31
|
+
|
|
32
|
+
Add them to the route configuration as an array. They execute in order.
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
{
|
|
36
|
+
path: 'admin',
|
|
37
|
+
component: Admin,
|
|
38
|
+
canActivate: [authGuard],
|
|
39
|
+
canActivateChild: [adminChildGuard],
|
|
40
|
+
canDeactivate: [unsavedChangesGuard]
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Return Values
|
|
45
|
+
|
|
46
|
+
- `boolean`: `true` to allow, `false` to block.
|
|
47
|
+
- `UrlTree` or `RedirectCommand`: Redirect to a different route.
|
|
48
|
+
- `Observable` or `Promise`: Resolves to the above types.
|
|
49
|
+
|
|
50
|
+
## Security Note
|
|
51
|
+
|
|
52
|
+
**Client-side guards are NOT a substitute for server-side security.** Always verify permissions on the server.
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Router Lifecycle and Events
|
|
2
|
+
|
|
3
|
+
Angular Router emits events through the `Router.events` observable, allowing you to track the navigation lifecycle from start to finish.
|
|
4
|
+
|
|
5
|
+
## Common Router Events (Chronological)
|
|
6
|
+
|
|
7
|
+
1. **`NavigationStart`**: Navigation begins.
|
|
8
|
+
2. **`RoutesRecognized`**: Router matches the URL to a route.
|
|
9
|
+
3. **`GuardsCheckStart` / `End`**: Evaluation of `canActivate`, `canMatch`, etc.
|
|
10
|
+
4. **`ResolveStart` / `End`**: Data resolution phase (fetching data via resolvers).
|
|
11
|
+
5. **`NavigationEnd`**: Navigation completed successfully.
|
|
12
|
+
6. **`NavigationCancel`**: Navigation canceled (e.g., guard returned `false`).
|
|
13
|
+
7. **`NavigationError`**: Navigation failed (e.g., error in resolver).
|
|
14
|
+
|
|
15
|
+
## Subscribing to Events
|
|
16
|
+
|
|
17
|
+
Inject the `Router` and filter the `events` observable.
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import {Router, NavigationStart, NavigationEnd} from '@angular/router';
|
|
21
|
+
|
|
22
|
+
export class MyService {
|
|
23
|
+
private router = inject(Router);
|
|
24
|
+
|
|
25
|
+
constructor() {
|
|
26
|
+
this.router.events.pipe(filter((e) => e instanceof NavigationEnd)).subscribe((event) => {
|
|
27
|
+
console.log('Navigated to:', event.url);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Debugging
|
|
34
|
+
|
|
35
|
+
Enable detailed console logging of all routing events during application bootstrap.
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
provideRouter(routes, withDebugTracing());
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Common Use Cases
|
|
42
|
+
|
|
43
|
+
- **Loading Indicators**: Show a spinner when `NavigationStart` fires and hide it on `NavigationEnd`/`Cancel`/`Error`.
|
|
44
|
+
- **Analytics**: Track page views by listening for `NavigationEnd`.
|
|
45
|
+
- **Scroll Management**: Respond to `Scroll` events for custom scroll behavior.
|