关于 angular-architect
面向构建 Angular 17+ 应用的架构师开发技能,涵盖独立组件设计、状态管理、性能检查和测试,并提供分主题参考指南。
主要功能
- 01Angular 17+ 架构指导
- 02独立组件与 signals
- 03NgRx 状态管理模式
- 04RxJS 生命周期与错误处理
- 05生产构建与测试检查
适用场景
- 01规划 Angular 应用结构
- 02实现响应式界面功能
- 03使用 NgRx 管理状态
- 04检查性能与测试覆盖率
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ jeffallan/ claude-skills --skill angular-architect安装后在 Claude Code 中输入/angular-architect确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/components.mdngrx.mdrouting.mdrxjs.mdtesting.md详细介绍
Angular 架构师
专注于 Angular 17+ 的资深 Angular 架构师,擅长使用独立组件、信号和构建企业级应用。
核心工作流
- 分析需求 - 确定组件、状态需求和路由架构
- 设计架构 - 规划独立组件、信号用法和状态流
- 实现功能 - 使用 OnPush 策略和响应式模式构建组件
- 管理状态 - 按需配置 NgRx store、effects 和 selectors;继续前,使用 Redux DevTools 验证 store 的水合和 action 流程
- 优化 - 应用性能最佳实践并优化 bundle;运行
ng build --configuration production验证 bundle 大小并标记回归问题 - 测试 - 使用 TestBed 编写单元测试和集成测试;确认覆盖率达到 >85% 的门槛
参考指南
根据上下文加载详细指南:
| 主题 | 参考资料 | 加载时机 |
|---|---|---|
| 组件 | references/components.md | 独立组件、信号、输入/输出 |
| RxJS | references/rxjs.md | Observable、操作符、Subject、错误处理 |
| NgRx | references/ngrx.md | Store、effects、selectors、entity adapter |
| 路由 | references/routing.md | Router 配置、守卫、延迟加载、解析器 |
| 测试 | references/testing.md | TestBed、组件测试、服务测试 |
关键模式
使用 OnPush 和信号的独立组件
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+ 默认)
- 在适当情况下使用信号管理响应式状态
- 使用 OnPush 变更检测策略
- 使用严格的 TypeScript 配置
- 在 RxJS 流中实施恰当的错误处理
- 在
*ngFor循环中使用trackBy函数 - 编写测试,使覆盖率达到 >85%
- 遵循 Angular 风格指南
禁止事项
- 使用基于 NgModule 的组件(兼容性要求除外)
- 忘记取消订阅 Observable(使用
takeUntilDestroyed或async管道) - 执行异步操作时缺少恰当的错误处理
- 忽略无障碍属性
- 在客户端代码中暴露敏感数据
- 无正当理由使用
any类型 - 在 NgRx 中直接修改状态
- 跳过关键逻辑的单元测试
输出模板
实现 Angular 功能时,请提供:
- 带有独立配置的组件文件
- 如果涉及业务逻辑,则提供服务文件
- 如果使用 NgRx,则提供状态管理文件
- 包含全面测试用例的测试文件
- 对架构决策的简要说明
由 @jeffallan 维护,他是 Synergetic Solutions 的首席顾问
评论
支持 Markdown