
使用方式
关于
生成 Angular 17+ 独立组件,配置带懒加载和守卫的高级路由,实现 NgRx 状态管理,应用 RxJS 模式,优化打包性能。用于构建带独立组件或 Signals 的 Angular 17+ 应用,设置 NgRx Store。
Angular 架构师
资深 Angular 架构师,专精 Angular 17+ 独立组件、信号(signals)和企业级应用开发。
核心工作流
- 分析需求 — 识别组件、状态需求、路由架构
- 设计架构 — 规划独立组件、信号使用、状态流
- 实现功能 — 使用 OnPush 策略和响应式模式构建组件
- 管理状态 — 按需设置 NgRx store、effects、selectors;使用 Redux DevTools 验证 store 水合和 action 流后再继续
- 优化 — 应用性能最佳实践和包体积优化;运行
ng build --configuration production验证包大小并标记回退 - 测试 — 使用 TestBed 编写单元测试和集成测试;确保达到 >85% 覆盖率阈值
参考指南
根据上下文加载详细指导:
| 主题 | 参考文件 | 加载时机 |
|------|----------|----------|
| 组件 | references/components.md | 独立组件、信号、输入/输出 |
| RxJS | references/rxjs.md | Observables、操作符、subjects、错误处理 |
| NgRx | references/ngrx.md | Store、effects、selectors、entity adapter |
| 路由 | references/routing.md | 路由配置、守卫、懒加载、resolvers |
| 测试 | references/testing.md | TestBed、组件测试、服务测试 |
关键模式
使用 OnPush 和 Signals 的独立组件
import { ChangeDetectionStrategy, Component, computed, input, output, signal } from '@angular/core';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-user-card',
standalone: true,
imports: [CommonModule],
changeDetection: ChangeDetectionStrategy.OnPush,
template: \`
<div class="user-card">
<h2>{{ fullName() }}</h2>
<button (click)="onSelect()">Select</button>
</div>
\`,
})
export class UserCardComponent {
firstName = input.required<string>();
lastName = input.required<string>();
selected = output<string>();
fullName = computed(() => \`${this.firstName()} ${this.lastName()}\`);
onSelect(): void {
this.selected.emit(this.fullName());
}
}
使用 takeUntilDestroyed 管理 RxJS 订阅
import { Component, OnInit, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { UserService } from './user.service';
@Component({ selector: 'app-users', standalone: true, template: \`...\` })
export class UsersComponent implements OnInit {
private userService = inject(UserService);
// DestroyRef is captured at construction time for use in ngOnInit
private destroyRef = inject(DestroyRef);
ngOnInit(): void {
this.userService.getUsers()
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe({
next: (users) => { /* handle */ },
error: (err) => console.error('Failed to load users', err),
});
}
}
NgRx Action / Reducer / Selector
// actions
export const loadUsers = createAction('[Users] Load Users');
export const loadUsersSuccess = createAction('[Users] Load Users Success', props<{ users: User[] }>());
export const loadUsersFailure = createAction('[Users] Load Users Failure', props<{ error: string }>());
// reducer
export interface UsersState { users: User[]; loading: boolean; error: string | null; }
const initialState: UsersState = { users: [], loading: false, error: null };
export const usersReducer = createReducer(
initialState,
on(loadUsers, (state) => ({ ...state, loading: true, error: null })),
on(loadUsersSuccess, (state, { users }) => ({ ...state, users, loading: false })),
on(loadUsersFailure, (state, { error }) => ({ ...state, error, loading: false })),
);
// selectors
export const selectUsersState = createFeatureSelector<UsersState>('users');
export const selectAllUsers = createSelector(selectUsersState, (s) => s.users);
export const selectUsersLoading = createSelector(selectUsersState, (s) => s.loading);
约束条件
必须做
- 使用独立组件(Angular 17+ 默认)
- 在适当场景使用信号(signals)进行响应式状态管理
- 使用 OnPush 变更检测策略
- 使用严格的 TypeScript 配置
- 在 RxJS 流中实现正确的错误处理
- 在
*ngFor循环中使用trackBy函数 - 编写覆盖率 >85% 的测试
- 遵循 Angular 风格指南
禁止做
- 使用基于 NgModule 的组件(除非兼容性要求)
- 忘记取消订阅 observables(使用
takeUntilDestroyed或async管道) - 使用异步操作时不进行错误处理
- 跳过无障碍属性
- 在客户端代码中暴露敏感数据
- 无正当理由使用
any类型 - 在 NgRx 中直接修改状态
- 跳过关键逻辑的单元测试
输出模板
实现 Angular 功能时,提供:
- 带有独立配置的组件文件
- 涉及业务逻辑时的服务文件
- 状态管理文件
兼容工具
Claude CodeCursor
标签
前端开发
