angular-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAngular Best Practices
Angular 最佳实践
Comprehensive performance optimization guide for Angular applications, containing 45+ rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
这是一份针对Angular应用的全面性能优化指南,包含8个分类下的45+条规则,按影响优先级排序,可指导自动化重构与代码生成。
When to Apply
适用场景
Reference these guidelines when:
- Writing new Angular components or services
- Implementing change detection strategies
- Reviewing code for performance issues
- Refactoring existing Angular code
- Optimizing bundle size or load times
- Working with RxJS observables and state management
- Implementing forms and validation
- Writing unit and integration tests
在以下场景中可参考本指南:
- 编写新的Angular组件或服务
- 实现变更检测策略
- 评审代码以排查性能问题
- 重构现有Angular代码
- 优化包体积或加载时间
- 处理RxJS可观察对象与状态管理
- 实现表单与验证
- 编写单元测试与集成测试
Rule Categories by Priority
按优先级划分的规则分类
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Change Detection | CRITICAL | |
| 2 | Bundle Size Optimization | CRITICAL | |
| 3 | Template Performance | HIGH | |
| 4 | RxJS & Async Operations | HIGH | |
| 5 | Component Architecture | MEDIUM-HIGH | |
| 6 | HTTP & Data Fetching | MEDIUM | |
| 7 | Forms & Validation | MEDIUM | |
| 8 | Testing & Debugging | LOW-MEDIUM | |
| 优先级 | 分类 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 变更检测 | 关键 | |
| 2 | 包体积优化 | 关键 | |
| 3 | 模板性能 | 高 | |
| 4 | RxJS 与异步操作 | 高 | |
| 5 | 组件架构 | 中高 | |
| 6 | HTTP 与数据获取 | 中 | |
| 7 | 表单与验证 | 中 | |
| 8 | 测试与调试 | 中低 | |
Quick Reference
快速参考
1. Change Detection (CRITICAL)
1. 变更检测(关键)
- - Use OnPush change detection strategy
cd-onpush - - Always use trackBy in *ngFor
cd-trackby - - Prefer pure pipes over methods in templates
cd-pure-pipes - - Use immutable data patterns
cd-immutable-data - - Detach change detection for heavy computations
cd-detach-reattach - - Run non-UI code outside NgZone
cd-run-outside-angular
- - 使用OnPush变更检测策略
cd-onpush - - 在*ngFor中始终使用trackBy
cd-trackby - - 优先在模板中使用纯管道而非方法
cd-pure-pipes - - 使用不可变数据模式
cd-immutable-data - - 针对繁重计算,分离并重新附加变更检测
cd-detach-reattach - - 在NgZone之外运行非UI代码
cd-run-outside-angular
2. Bundle Size Optimization (CRITICAL)
2. 包体积优化(关键)
- - Lazy load feature modules
bundle-lazy-loading - - Ensure tree-shakeable providers
bundle-tree-shaking - - Use standalone components
bundle-standalone-components - - Use @defer for heavy components
bundle-defer-views - - Implement smart preloading
bundle-preload-strategies - - Import directly, avoid barrel files
bundle-avoid-barrel-exports
- - 懒加载功能模块
bundle-lazy-loading - - 确保提供者支持摇树优化
bundle-tree-shaking - - 使用独立组件
bundle-standalone-components - - 为重型组件使用@defer
bundle-defer-views - - 实现智能预加载策略
bundle-preload-strategies - - 直接导入,避免使用桶文件
bundle-avoid-barrel-exports
3. Template Performance (HIGH)
3. 模板性能(高)
- - Avoid function calls in templates
template-avoid-function-calls - - Use async pipe instead of manual subscriptions
template-async-pipe - - Use ng-container for structural directives
template-ng-container - - Use new control flow syntax (@if, @for)
template-control-flow - - Never use *ngIf and *ngFor on same element
template-optimize-ngif-ngfor - - Use NgOptimizedImage directive
template-image-optimization
- - 避免在模板中调用函数
template-avoid-function-calls - - 使用async管道而非手动订阅
template-async-pipe - - 为结构型指令使用ng-container
template-ng-container - - 使用新的控制流语法(@if、@for)
template-control-flow - - 切勿在同一元素上同时使用ngIf和ngFor
template-optimize-ngif-ngfor - - 使用NgOptimizedImage指令
template-image-optimization
4. RxJS & Async Operations (HIGH)
4. RxJS 与异步操作(高)
- - Never nest subscriptions
rxjs-avoid-nested-subscriptions - - Always unsubscribe (takeUntilDestroyed, DestroyRef)
rxjs-unsubscribe - - Use shareReplay for HTTP caching
rxjs-share-replay - - Choose correct flattening operator
rxjs-switchmap-vs-mergemap - - Prefer signals for synchronous state
rxjs-signals-vs-observables - - Debounce user input events
rxjs-debounce-throttle
- - 切勿嵌套订阅
rxjs-avoid-nested-subscriptions - - 始终取消订阅(使用takeUntilDestroyed、DestroyRef)
rxjs-unsubscribe - - 使用shareReplay实现HTTP缓存
rxjs-share-replay - - 选择正确的展平操作符
rxjs-switchmap-vs-mergemap - - 针对同步状态优先使用Signals
rxjs-signals-vs-observables - - 对用户输入事件进行防抖处理
rxjs-debounce-throttle
5. Component Architecture (MEDIUM-HIGH)
5. 组件架构(中高)
- - Separate smart and presentational components
component-smart-presentational - - Use input transforms for data conversion
component-input-transforms - - Follow output naming conventions
component-output-naming - - Use content projection effectively
component-content-projection - - Load components dynamically when needed
component-dynamic-components - - Compose behavior with host directives
component-host-directives
- - 分离智能组件与展示组件
component-smart-presentational - - 使用输入转换进行数据转换
component-input-transforms - - 遵循输出命名规范
component-output-naming - - 有效使用内容投影
component-content-projection - - 在需要时动态加载组件
component-dynamic-components - - 使用宿主指令组合行为
component-host-directives
6. HTTP & Data Fetching (MEDIUM)
6. HTTP 与数据获取(中)
- - Use interceptors for cross-cutting concerns
http-interceptors - - Implement HTTP caching strategies
http-caching - - Add retry logic for resilience
http-retry-logic - - Cancel pending requests on navigation
http-cancel-requests - - Always type HTTP responses
http-typed-responses - - Implement centralized error handling
http-error-handling
- - 使用拦截器处理横切关注点
http-interceptors - - 实现HTTP缓存策略
http-caching - - 添加重试逻辑以提升韧性
http-retry-logic - - 在导航时取消待处理请求
http-cancel-requests - - 始终为HTTP响应添加类型
http-typed-responses - - 实现集中式错误处理
http-error-handling
7. Forms & Validation (MEDIUM)
7. 表单与验证(中)
- - Prefer reactive forms for complex forms
forms-reactive-over-template - - Use strictly typed reactive forms
forms-typed-forms - - Create reusable custom validators
forms-custom-validators - - Debounce async validators
forms-async-validation - - Implement ControlValueAccessor correctly
forms-control-value-accessor - - Handle dynamic form arrays efficiently
forms-form-arrays
- - 复杂表单优先使用响应式表单
forms-reactive-over-template - - 使用严格类型的响应式表单
forms-typed-forms - - 创建可复用的自定义验证器
forms-custom-validators - - 对异步验证器进行防抖处理
forms-async-validation - - 正确实现ControlValueAccessor
forms-control-value-accessor - - 高效处理动态表单数组
forms-form-arrays
8. Testing & Debugging (LOW-MEDIUM)
8. 测试与调试(中低)
- - Use component harnesses
testing-component-harness - - Mock services properly
testing-mock-services - - Use fakeAsync/tick for async tests
testing-async-testing - - Trigger change detection in tests
testing-change-detection - - Use marble testing for RxJS
testing-marble-testing - - Profile with Angular DevTools
testing-performance-profiling
- - 使用组件测试工具(Component Harnesses)
testing-component-harness - - 正确模拟服务
testing-mock-services - - 使用fakeAsync/tick处理异步测试
testing-async-testing - - 在测试中触发变更检测
testing-change-detection - - 为RxJS使用大理石测试
testing-marble-testing - - 使用Angular DevTools进行性能分析
testing-performance-profiling
How to Use
使用方法
Read individual rule files for detailed explanations and code examples:
rules/cd-onpush.md
rules/bundle-lazy-loading.md
rules/_sections.mdEach rule file contains:
- Brief explanation of why it matters
- Incorrect code example with explanation
- Correct code example with explanation
- Additional context and references
阅读单个规则文件以获取详细说明和代码示例:
rules/cd-onpush.md
rules/bundle-lazy-loading.md
rules/_sections.md每个规则文件包含:
- 简要说明该规则的重要性
- 错误代码示例及解释
- 正确代码示例及解释
- 额外背景信息与参考资料
Full Compiled Document
完整编译文档
For the complete guide with all rules expanded:
AGENTS.md如需查看包含所有扩展规则的完整指南,请参阅:
AGENTS.md