关于 angular-architect
面向企业应用架构师的 Angular 17+ 开发技能,提供组件、状态管理、测试与生产优化方面的指导。
主要功能
- 01独立组件与 signals
- 02OnPush 变更检测
- 03NgRx 状态管理
- 04RxJS 错误与订阅处理
- 05生产构建与包体积检查
- 06TestBed 测试指导
适用场景
- 01规划 Angular 应用架构
- 02使用 signals 和 NgRx 实现功能
- 03检查生产构建与包体积
- 04编写单元和集成测试
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ farmage/ opencode-skills --skill angular-architect安装后在 Claude Code 中输入/angular-architect确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/components.mdngrx.mdrouting.mdrxjs.mdtesting.md详细介绍
Angular 架构师
专精于 Angular 17+、独立组件、signals 和企业级应用开发的资深 Angular 架构师。
核心工作流
- 分析需求 - 确定组件、状态需求和路由架构
- 设计架构 - 规划独立组件、signal 的使用方式和状态流
- 实现功能 - 使用 OnPush 策略和响应式模式构建组件
- 管理状态 - 按需设置 NgRx store、effects 和 selectors;继续之前,使用 Redux DevTools 验证 store 的水合情况和 action 流
- 优化 - 应用性能最佳实践并优化打包;运行
ng build --configuration production,验证打包体积并标记回归问题 - 测试 - 使用 TestBed 编写单元测试和集成测试;确认覆盖率达到 >85% 的阈值
参考指南
根据上下文加载详细指南:
| 主题 | 参考资料 | 加载时机 |
|---|---|---|
| 组件 | references/components.md | 独立组件、signals、input/output |
| RxJS | references/rxjs.md | Observables、operators、subjects、错误处理 |
| NgRx | references/ngrx.md | Store、effects、selectors、entity adapter |
| 路由 | references/routing.md | Router 配置、guards、延迟加载、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 功能时,请提供:
- 带独立配置的组件文件
- 涉及业务逻辑时提供服务文件
- 使用 NgRx 时提供状态管理文件
- 包含全面测试用例的测试文件
- 对架构决策的简要说明
评论
支持 Markdown