create-huenei-template 0.2.2 → 0.2.4
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/dist/config/templates.js +3 -3
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-component/SKILL.md +288 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-component/references/component-patterns.md +358 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-di/SKILL.md +387 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-di/references/di-patterns.md +519 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-directives/SKILL.md +437 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-directives/references/directive-patterns.md +570 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-forms/SKILL.md +434 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-forms/references/form-patterns.md +398 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-http/SKILL.md +366 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-http/references/http-patterns.md +448 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-routing/SKILL.md +395 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-routing/references/routing-patterns.md +472 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-signals/SKILL.md +302 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-signals/references/signal-patterns.md +404 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-ssr/SKILL.md +435 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-ssr/references/ssr-patterns.md +497 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-testing/SKILL.md +660 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-testing/references/testing-patterns.md +707 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-tooling/SKILL.md +352 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-tooling/references/tooling-patterns.md +448 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-accessibility/SKILL.md +780 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-components/SKILL.md +718 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-design-systems/SKILL.md +612 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-layouts/SKILL.md +594 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/SKILL.md +432 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/references/animation-patterns.md +427 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/SKILL.md +354 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/references/troubleshooting-guide.md +306 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/SKILL.md +578 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/references/react-patterns.md +353 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/SKILL.md +608 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/advanced-theme-patterns.md +215 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/custom-utilities-guide.md +281 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-mobile-first/SKILL.md +768 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/SKILL.md +374 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/references/bundle-optimization.md +244 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-plugins/SKILL.md +505 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/SKILL.md +601 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/references/advanced-responsive-patterns.md +485 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/web-design-guidelines/SKILL.md +39 -0
- package/dist/templates/frontend/angular-base/package.json +0 -1
- package/dist/templates/frontend/angular-base/pnpm-lock.yaml +8183 -0
- package/package.json +1 -1
- package/templates/frontend/angular-base/.cursor/skills/angular-component/SKILL.md +288 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-component/references/component-patterns.md +358 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-di/SKILL.md +387 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-di/references/di-patterns.md +519 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-directives/SKILL.md +437 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-directives/references/directive-patterns.md +570 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-forms/SKILL.md +434 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-forms/references/form-patterns.md +398 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-http/SKILL.md +366 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-http/references/http-patterns.md +448 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-routing/SKILL.md +395 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-routing/references/routing-patterns.md +472 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-signals/SKILL.md +302 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-signals/references/signal-patterns.md +404 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-ssr/SKILL.md +435 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-ssr/references/ssr-patterns.md +497 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-testing/SKILL.md +660 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-testing/references/testing-patterns.md +707 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-tooling/SKILL.md +352 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-tooling/references/tooling-patterns.md +448 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-accessibility/SKILL.md +780 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-components/SKILL.md +718 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-design-systems/SKILL.md +612 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-layouts/SKILL.md +594 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/SKILL.md +432 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/references/animation-patterns.md +427 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/SKILL.md +354 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/references/troubleshooting-guide.md +306 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/SKILL.md +578 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/references/react-patterns.md +353 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/SKILL.md +608 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/advanced-theme-patterns.md +215 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/custom-utilities-guide.md +281 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-mobile-first/SKILL.md +768 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/SKILL.md +374 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/references/bundle-optimization.md +244 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-plugins/SKILL.md +505 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/SKILL.md +601 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/references/advanced-responsive-patterns.md +485 -0
- package/templates/frontend/angular-base/.cursor/skills/web-design-guidelines/SKILL.md +39 -0
- package/templates/frontend/angular-base/package.json +0 -1
- package/templates/frontend/angular-base/pnpm-lock.yaml +8183 -0
- package/templates/frontend/angular-base/package-lock.json +0 -13321
|
@@ -0,0 +1,472 @@
|
|
|
1
|
+
# Angular Routing Patterns
|
|
2
|
+
|
|
3
|
+
## Table of Contents
|
|
4
|
+
- [Route Configuration Options](#route-configuration-options)
|
|
5
|
+
- [Authentication Flow](#authentication-flow)
|
|
6
|
+
- [Breadcrumbs](#breadcrumbs)
|
|
7
|
+
- [Tab Navigation](#tab-navigation)
|
|
8
|
+
- [Modal Routes](#modal-routes)
|
|
9
|
+
- [Preloading Strategies](#preloading-strategies)
|
|
10
|
+
|
|
11
|
+
## Route Configuration Options
|
|
12
|
+
|
|
13
|
+
### Full Route Options
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
{
|
|
17
|
+
path: 'users/:id',
|
|
18
|
+
component: UserCmpt,
|
|
19
|
+
|
|
20
|
+
// Lazy loading alternatives
|
|
21
|
+
loadComponent: () => import('./user.component').then(m => m.UserCmpt),
|
|
22
|
+
loadChildren: () => import('./user.routes').then(m => m.userRoutes),
|
|
23
|
+
|
|
24
|
+
// Guards
|
|
25
|
+
canActivate: [authGuard],
|
|
26
|
+
canActivateChild: [authGuard],
|
|
27
|
+
canDeactivate: [unsavedChangesGuard],
|
|
28
|
+
canMatch: [featureFlagGuard],
|
|
29
|
+
|
|
30
|
+
// Data
|
|
31
|
+
resolve: { user: userResolver },
|
|
32
|
+
data: { title: 'User Profile', animation: 'userPage' },
|
|
33
|
+
|
|
34
|
+
// Children
|
|
35
|
+
children: [...],
|
|
36
|
+
|
|
37
|
+
// Outlet
|
|
38
|
+
outlet: 'sidebar',
|
|
39
|
+
|
|
40
|
+
// Path matching
|
|
41
|
+
pathMatch: 'full', // or 'prefix'
|
|
42
|
+
|
|
43
|
+
// Title
|
|
44
|
+
title: 'User Profile',
|
|
45
|
+
// Or dynamic title
|
|
46
|
+
title: userTitleResolver,
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Dynamic Title Resolver
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
export const userTitleResolver: ResolveFn<string> = (route) => {
|
|
54
|
+
const userService = inject(User);
|
|
55
|
+
const id = route.paramMap.get('id')!;
|
|
56
|
+
return userService.getById(id).pipe(
|
|
57
|
+
map(user => `${user.name} - Profile`)
|
|
58
|
+
);
|
|
59
|
+
};
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Authentication Flow
|
|
63
|
+
|
|
64
|
+
### Complete Auth Setup
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
// auth.service.ts
|
|
68
|
+
@Injectable({ providedIn: 'root' })
|
|
69
|
+
export class Auth {
|
|
70
|
+
private _user = signal<User | null>(null);
|
|
71
|
+
private _token = signal<string | null>(null);
|
|
72
|
+
|
|
73
|
+
readonly user = this._user.asReadonly();
|
|
74
|
+
readonly isAuthenticated = computed(() => this._user() !== null);
|
|
75
|
+
|
|
76
|
+
private router = inject(Router);
|
|
77
|
+
private http = inject(HttpClient);
|
|
78
|
+
|
|
79
|
+
async login(credentials: Credentials): Promise<boolean> {
|
|
80
|
+
try {
|
|
81
|
+
const response = await firstValueFrom(
|
|
82
|
+
this.http.post<AuthResponse>('/api/login', credentials)
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
this._token.set(response.token);
|
|
86
|
+
this._user.set(response.user);
|
|
87
|
+
localStorage.setItem('token', response.token);
|
|
88
|
+
|
|
89
|
+
return true;
|
|
90
|
+
} catch {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
logout(): void {
|
|
96
|
+
this._user.set(null);
|
|
97
|
+
this._token.set(null);
|
|
98
|
+
localStorage.removeItem('token');
|
|
99
|
+
this.router.navigate(['/login']);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async checkAuth(): Promise<boolean> {
|
|
103
|
+
const token = localStorage.getItem('token');
|
|
104
|
+
if (!token) return false;
|
|
105
|
+
|
|
106
|
+
try {
|
|
107
|
+
const user = await firstValueFrom(
|
|
108
|
+
this.http.get<User>('/api/me')
|
|
109
|
+
);
|
|
110
|
+
this._user.set(user);
|
|
111
|
+
this._token.set(token);
|
|
112
|
+
return true;
|
|
113
|
+
} catch {
|
|
114
|
+
localStorage.removeItem('token');
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// auth.guard.ts
|
|
121
|
+
export const authGuard: CanActivateFn = async (route, state) => {
|
|
122
|
+
const authService = inject(Auth);
|
|
123
|
+
const router = inject(Router);
|
|
124
|
+
|
|
125
|
+
// Check if already authenticated
|
|
126
|
+
if (authService.isAuthenticated()) {
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Try to restore session
|
|
131
|
+
const isValid = await authService.checkAuth();
|
|
132
|
+
if (isValid) {
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Redirect to login
|
|
137
|
+
return router.createUrlTree(['/login'], {
|
|
138
|
+
queryParams: { returnUrl: state.url },
|
|
139
|
+
});
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
// login.component.ts
|
|
143
|
+
@Component({
|
|
144
|
+
template: `
|
|
145
|
+
<form (ngSubmit)="login()">
|
|
146
|
+
<input [(ngModel)]="email" name="email" />
|
|
147
|
+
<input [(ngModel)]="password" name="password" type="password" />
|
|
148
|
+
<button type="submit">Login</button>
|
|
149
|
+
</form>
|
|
150
|
+
`,
|
|
151
|
+
})
|
|
152
|
+
export class Login {
|
|
153
|
+
private authService = inject(Auth);
|
|
154
|
+
private router = inject(Router);
|
|
155
|
+
private route = inject(ActivatedRoute);
|
|
156
|
+
|
|
157
|
+
email = '';
|
|
158
|
+
password = '';
|
|
159
|
+
|
|
160
|
+
async login() {
|
|
161
|
+
const success = await this.authService.login({
|
|
162
|
+
email: this.email,
|
|
163
|
+
password: this.password,
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
if (success) {
|
|
167
|
+
const returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
|
|
168
|
+
this.router.navigateByUrl(returnUrl);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## Breadcrumbs
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
// breadcrumb.service.ts
|
|
178
|
+
@Injectable({ providedIn: 'root' })
|
|
179
|
+
export class Breadcrumb {
|
|
180
|
+
private router = inject(Router);
|
|
181
|
+
private route = inject(ActivatedRoute);
|
|
182
|
+
|
|
183
|
+
breadcrumbs = toSignal(
|
|
184
|
+
this.router.events.pipe(
|
|
185
|
+
filter(event => event instanceof NavigationEnd),
|
|
186
|
+
map(() => this.buildBreadcrumbs(this.route.root))
|
|
187
|
+
),
|
|
188
|
+
{ initialValue: [] }
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
private buildBreadcrumbs(
|
|
192
|
+
route: ActivatedRoute,
|
|
193
|
+
url: string = '',
|
|
194
|
+
breadcrumbs: Breadcrumb[] = []
|
|
195
|
+
): Breadcrumb[] {
|
|
196
|
+
const children = route.children;
|
|
197
|
+
|
|
198
|
+
if (children.length === 0) {
|
|
199
|
+
return breadcrumbs;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
for (const child of children) {
|
|
203
|
+
const routeUrl = child.snapshot.url
|
|
204
|
+
.map(segment => segment.path)
|
|
205
|
+
.join('/');
|
|
206
|
+
|
|
207
|
+
if (routeUrl) {
|
|
208
|
+
url += `/${routeUrl}`;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const label = child.snapshot.data['breadcrumb'];
|
|
212
|
+
if (label) {
|
|
213
|
+
breadcrumbs.push({ label, url });
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return this.buildBreadcrumbs(child, url, breadcrumbs);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return breadcrumbs;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Route config with breadcrumb data
|
|
224
|
+
export const routes: Routes = [
|
|
225
|
+
{
|
|
226
|
+
path: 'products',
|
|
227
|
+
data: { breadcrumb: 'Products' },
|
|
228
|
+
children: [
|
|
229
|
+
{ path: '', component: ProductList },
|
|
230
|
+
{
|
|
231
|
+
path: ':id',
|
|
232
|
+
data: { breadcrumb: 'Product Details' },
|
|
233
|
+
component: ProductDetail,
|
|
234
|
+
},
|
|
235
|
+
],
|
|
236
|
+
},
|
|
237
|
+
];
|
|
238
|
+
|
|
239
|
+
// breadcrumb.component.ts
|
|
240
|
+
@Component({
|
|
241
|
+
selector: 'app-breadcrumb',
|
|
242
|
+
template: `
|
|
243
|
+
<nav aria-label="Breadcrumb">
|
|
244
|
+
<ol>
|
|
245
|
+
<li><a routerLink="/">Home</a></li>
|
|
246
|
+
@for (crumb of breadcrumbService.breadcrumbs(); track crumb.url) {
|
|
247
|
+
<li>
|
|
248
|
+
<a [routerLink]="crumb.url">{{ crumb.label }}</a>
|
|
249
|
+
</li>
|
|
250
|
+
}
|
|
251
|
+
</ol>
|
|
252
|
+
</nav>
|
|
253
|
+
`,
|
|
254
|
+
})
|
|
255
|
+
export class BreadcrumbCmpt {
|
|
256
|
+
breadcrumbService = inject(Breadcrumb);
|
|
257
|
+
}
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
## Tab Navigation
|
|
261
|
+
|
|
262
|
+
```typescript
|
|
263
|
+
// tabs-layout.component.ts
|
|
264
|
+
@Component({
|
|
265
|
+
imports: [RouterOutlet, RouterLink, RouterLinkActive],
|
|
266
|
+
template: `
|
|
267
|
+
<div class="tabs">
|
|
268
|
+
@for (tab of tabs; track tab.path) {
|
|
269
|
+
<a
|
|
270
|
+
[routerLink]="tab.path"
|
|
271
|
+
routerLinkActive="active"
|
|
272
|
+
[routerLinkActiveOptions]="{ exact: tab.exact }"
|
|
273
|
+
>
|
|
274
|
+
{{ tab.label }}
|
|
275
|
+
</a>
|
|
276
|
+
}
|
|
277
|
+
</div>
|
|
278
|
+
<div class="tab-content">
|
|
279
|
+
<router-outlet />
|
|
280
|
+
</div>
|
|
281
|
+
`,
|
|
282
|
+
})
|
|
283
|
+
export class TabsLayout {
|
|
284
|
+
tabs = [
|
|
285
|
+
{ path: './', label: 'Overview', exact: true },
|
|
286
|
+
{ path: 'details', label: 'Details', exact: false },
|
|
287
|
+
{ path: 'settings', label: 'Settings', exact: false },
|
|
288
|
+
];
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// Routes
|
|
292
|
+
{
|
|
293
|
+
path: 'account',
|
|
294
|
+
component: TabsLayout,
|
|
295
|
+
children: [
|
|
296
|
+
{ path: '', component: AccountOverview },
|
|
297
|
+
{ path: 'details', component: AccountDetails },
|
|
298
|
+
{ path: 'settings', component: AccountSettings },
|
|
299
|
+
],
|
|
300
|
+
}
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
## Modal Routes
|
|
304
|
+
|
|
305
|
+
Using auxiliary outlets for modals:
|
|
306
|
+
|
|
307
|
+
```typescript
|
|
308
|
+
// Routes
|
|
309
|
+
export const routes: Routes = [
|
|
310
|
+
{ path: 'products', component: ProductList },
|
|
311
|
+
{ path: 'product-modal/:id', component: ProductModal, outlet: 'modal' },
|
|
312
|
+
];
|
|
313
|
+
|
|
314
|
+
// App template
|
|
315
|
+
@Component({
|
|
316
|
+
template: `
|
|
317
|
+
<router-outlet />
|
|
318
|
+
<router-outlet name="modal" />
|
|
319
|
+
`,
|
|
320
|
+
})
|
|
321
|
+
export class App {}
|
|
322
|
+
|
|
323
|
+
// Open modal
|
|
324
|
+
this.router.navigate([{ outlets: { modal: ['product-modal', productId] } }]);
|
|
325
|
+
|
|
326
|
+
// Close modal
|
|
327
|
+
this.router.navigate([{ outlets: { modal: null } }]);
|
|
328
|
+
|
|
329
|
+
// Link to open modal
|
|
330
|
+
<a [routerLink]="[{ outlets: { modal: ['product-modal', product.id] } }]">
|
|
331
|
+
View Details
|
|
332
|
+
</a>
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
## Preloading Strategies
|
|
336
|
+
|
|
337
|
+
### Built-in Strategies
|
|
338
|
+
|
|
339
|
+
```typescript
|
|
340
|
+
import {
|
|
341
|
+
provideRouter,
|
|
342
|
+
withPreloading,
|
|
343
|
+
PreloadAllModules,
|
|
344
|
+
NoPreloading
|
|
345
|
+
} from '@angular/router';
|
|
346
|
+
|
|
347
|
+
// Preload all lazy modules
|
|
348
|
+
provideRouter(routes, withPreloading(PreloadAllModules))
|
|
349
|
+
|
|
350
|
+
// No preloading (default)
|
|
351
|
+
provideRouter(routes, withPreloading(NoPreloading))
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
### Custom Preloading Strategy
|
|
355
|
+
|
|
356
|
+
```typescript
|
|
357
|
+
// selective-preload.strategy.ts
|
|
358
|
+
@Injectable({ providedIn: 'root' })
|
|
359
|
+
export class SelectivePreloadStrategy implements PreloadingStrategy {
|
|
360
|
+
preload(route: Route, load: () => Observable<any>): Observable<any> {
|
|
361
|
+
// Only preload routes marked with data.preload = true
|
|
362
|
+
if (route.data?.['preload']) {
|
|
363
|
+
return load();
|
|
364
|
+
}
|
|
365
|
+
return of(null);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// Routes
|
|
370
|
+
{
|
|
371
|
+
path: 'dashboard',
|
|
372
|
+
loadComponent: () => import('./dashboard.component'),
|
|
373
|
+
data: { preload: true }, // Will be preloaded
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// Config
|
|
377
|
+
provideRouter(routes, withPreloading(SelectivePreloadStrategy))
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
### Network-Aware Preloading
|
|
381
|
+
|
|
382
|
+
```typescript
|
|
383
|
+
@Injectable({ providedIn: 'root' })
|
|
384
|
+
export class NetworkAwarePreloadStrategy implements PreloadingStrategy {
|
|
385
|
+
preload(route: Route, load: () => Observable<any>): Observable<any> {
|
|
386
|
+
// Check network conditions
|
|
387
|
+
const connection = (navigator as any).connection;
|
|
388
|
+
|
|
389
|
+
if (connection) {
|
|
390
|
+
// Don't preload on slow connections
|
|
391
|
+
if (connection.saveData || connection.effectiveType === '2g') {
|
|
392
|
+
return of(null);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// Preload if marked
|
|
397
|
+
if (route.data?.['preload']) {
|
|
398
|
+
return load();
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
return of(null);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
## Route Animations
|
|
407
|
+
|
|
408
|
+
```typescript
|
|
409
|
+
// app.routes.ts
|
|
410
|
+
export const routes: Routes = [
|
|
411
|
+
{ path: 'home', component: Home, data: { animation: 'HomePage' } },
|
|
412
|
+
{ path: 'about', component: About, data: { animation: 'AboutPage' } },
|
|
413
|
+
];
|
|
414
|
+
|
|
415
|
+
// app.component.ts
|
|
416
|
+
@Component({
|
|
417
|
+
imports: [RouterOutlet],
|
|
418
|
+
template: `
|
|
419
|
+
<div [@routeAnimations]="getRouteAnimationData()">
|
|
420
|
+
<router-outlet />
|
|
421
|
+
</div>
|
|
422
|
+
`,
|
|
423
|
+
animations: [
|
|
424
|
+
trigger('routeAnimations', [
|
|
425
|
+
transition('HomePage <=> AboutPage', [
|
|
426
|
+
style({ position: 'relative' }),
|
|
427
|
+
query(':enter, :leave', [
|
|
428
|
+
style({
|
|
429
|
+
position: 'absolute',
|
|
430
|
+
top: 0,
|
|
431
|
+
left: 0,
|
|
432
|
+
width: '100%',
|
|
433
|
+
}),
|
|
434
|
+
]),
|
|
435
|
+
query(':enter', [style({ left: '-100%' })]),
|
|
436
|
+
query(':leave', animateChild()),
|
|
437
|
+
group([
|
|
438
|
+
query(':leave', [animate('300ms ease-out', style({ left: '100%' }))]),
|
|
439
|
+
query(':enter', [animate('300ms ease-out', style({ left: '0%' }))]),
|
|
440
|
+
]),
|
|
441
|
+
]),
|
|
442
|
+
]),
|
|
443
|
+
],
|
|
444
|
+
})
|
|
445
|
+
export class AppMain {
|
|
446
|
+
getRouteAnimationData() {
|
|
447
|
+
return this.route.firstChild?.snapshot.data['animation'];
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
## Scroll Position Restoration
|
|
453
|
+
|
|
454
|
+
```typescript
|
|
455
|
+
// app.config.ts
|
|
456
|
+
import {
|
|
457
|
+
provideRouter,
|
|
458
|
+
withInMemoryScrolling,
|
|
459
|
+
withRouterConfig
|
|
460
|
+
} from '@angular/router';
|
|
461
|
+
|
|
462
|
+
provideRouter(
|
|
463
|
+
routes,
|
|
464
|
+
withInMemoryScrolling({
|
|
465
|
+
scrollPositionRestoration: 'enabled', // or 'top'
|
|
466
|
+
anchorScrolling: 'enabled',
|
|
467
|
+
}),
|
|
468
|
+
withRouterConfig({
|
|
469
|
+
onSameUrlNavigation: 'reload',
|
|
470
|
+
})
|
|
471
|
+
)
|
|
472
|
+
```
|