angular-best-practices-legacy
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAngular Best Practices (Legacy: v12-16)
Angular最佳实践(旧版本:v12-16)
Comprehensive performance optimization guide for Angular 12-16 applications using NgModule-based architecture and RxJS-centric patterns. Contains 30+ rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
针对采用NgModule架构和RxJS核心模式的Angular 12-16应用的综合性能优化指南。包含8大类共30+条规则,按影响优先级排序,可用于指导自动化重构和代码生成。
When to Apply
适用场景
Reference these guidelines when:
- Maintaining Angular 12-16 codebases
- Using NgModule-based architecture
- Working with *ngIf, *ngFor directives
- Using RxJS for state management
- Using class-based HTTP interceptors
- Reviewing code for performance issues
当你遇到以下情况时可参考本指南:
- 维护Angular 12-16代码库
- 使用基于NgModule的架构
- 处理*ngIf、*ngFor指令
- 使用RxJS做状态管理
- 使用基于类的HTTP拦截器
- 排查代码性能问题
Key Patterns (v12-16)
核心模式(v12-16)
- NgModules - Feature modules for organization
- BehaviorSubject - Reactive state management
- Subject + takeUntil - Subscription cleanup pattern
- Class-based interceptors - HTTP handling with implements HttpInterceptor
- **ngFor / ngIf - Structural directives with trackBy
- loadChildren - Module-based lazy loading
- NgModules - 用特性模块做代码组织
- BehaviorSubject - 响应式状态管理
- Subject + takeUntil - 订阅清理模式
- 基于类的拦截器 - 实现HttpInterceptor接口来处理HTTP请求
- **ngFor / ngIf - 搭配trackBy使用的结构型指令
- loadChildren - 基于模块的懒加载
Rule Categories by Priority
按优先级排序的规则分类
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Change Detection | CRITICAL | |
| 2 | Bundle & Lazy Loading | CRITICAL | |
| 3 | RxJS Optimization | HIGH | |
| 4 | Template Performance | HIGH | |
| 5 | Dependency Injection | MEDIUM-HIGH | |
| 6 | HTTP & Caching | MEDIUM | |
| 7 | Forms Optimization | MEDIUM | |
| 8 | General Performance | LOW-MEDIUM | |
| 优先级 | 分类 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 变更检测 | 紧急 | |
| 2 | 包体积与懒加载 | 紧急 | |
| 3 | RxJS优化 | 高 | |
| 4 | 模板性能 | 高 | |
| 5 | 依赖注入 | 中高 | |
| 6 | HTTP与缓存 | 中 | |
| 7 | 表单优化 | 中 | |
| 8 | 通用性能 | 中低 | |
Quick Reference
快速参考
1. Change Detection (CRITICAL)
1. 变更检测(紧急)
- - Use BehaviorSubject with OnPush for reactive state
change-rxjs-state - - Use OnPush change detection strategy
change-onpush - - Detach change detector for heavy operations
change-detach-reattach - - Run non-UI code outside NgZone
change-run-outside-zone
- - 搭配OnPush策略使用BehaviorSubject实现响应式状态
change-rxjs-state - - 使用OnPush变更检测策略
change-onpush - - 执行重负载操作前分离变更检测器
change-detach-reattach - - 在NgZone外运行非UI相关代码
change-run-outside-zone
2. Bundle & Lazy Loading (CRITICAL)
2. 包体积与懒加载(紧急)
- - Organize code into feature NgModules
bundle-ngmodule - - Use Single Component Angular Modules pattern
bundle-scam - - Lazy load routes with loadChildren
bundle-lazy-routes - - Preload routes for perceived speed
bundle-preload - - Avoid barrel files, use direct imports
bundle-no-barrel-imports
- - 将代码组织为特性NgModules
bundle-ngmodule - - 使用单组件Angular模块(SCAM)模式
bundle-scam - - 用loadChildren实现路由懒加载
bundle-lazy-routes - - 预加载路由提升感知速度
bundle-preload - - 避免桶文件,使用直接导入
bundle-no-barrel-imports
3. RxJS Optimization (HIGH)
3. RxJS优化(高)
- - Use async pipe instead of manual subscriptions
rxjs-async-pipe - - Use takeUntil with destroy$ Subject for cleanup
rxjs-takeuntil - - Share observables to avoid duplicate requests
rxjs-share-replay - - Use efficient RxJS operators
rxjs-operators - - Use correct mapping operators (switchMap vs exhaustMap)
rxjs-mapping-operators - - Avoid nested subscriptions
rxjs-no-nested-subscribe
- - 使用async管道替代手动订阅
rxjs-async-pipe - - 搭配destroy$ Subject使用takeUntil做订阅清理
rxjs-takeuntil - - 共享observable避免重复请求
rxjs-share-replay - - 使用高效的RxJS操作符
rxjs-operators - - 使用正确的映射操作符(switchMap vs exhaustMap)
rxjs-mapping-operators - - 避免嵌套订阅
rxjs-no-nested-subscribe
4. Template Performance (HIGH)
4. 模板性能(高)
- - Use trackBy function with *ngFor
template-trackby - - Use pure pipes for expensive transformations
template-pure-pipes - - Use NgOptimizedImage for image optimization
template-ng-optimized-image - - Avoid function calls in templates
template-no-function-calls - - Use virtual scrolling for large lists
template-virtual-scroll
- - *ngFor搭配trackBy函数使用
template-trackby - - 对耗时转换使用纯管道
template-pure-pipes - - 使用NgOptimizedImage做图片优化
template-ng-optimized-image - - 避免在模板中调用函数
template-no-function-calls - - 长列表使用虚拟滚动
template-virtual-scroll
5. Dependency Injection (MEDIUM-HIGH)
5. 依赖注入(中高)
- - Use providedIn: 'root' for singleton services
di-provided-in-root - - Use InjectionToken for non-class dependencies
di-injection-token - - Use factory providers for complex initialization
di-factory-providers
- - 单例服务使用providedIn: 'root'
di-provided-in-root - - 非类依赖使用InjectionToken
di-injection-token - - 复杂初始化使用工厂提供者
di-factory-providers
6. HTTP & Caching (MEDIUM)
6. HTTP与缓存(中)
- - Use class-based interceptors for cross-cutting concerns
http-interceptors - - Use TransferState for SSR hydration
http-transfer-state
- - 用基于类的拦截器处理横切关注点
http-interceptors - - SSR hydration使用TransferState
http-transfer-state
7. Forms Optimization (MEDIUM)
7. 表单优化(中)
- - Use reactive forms instead of template-driven
forms-reactive - - Use typed FormGroup for type safety
forms-typed
- - 使用响应式表单替代模板驱动表单
forms-reactive - - 使用带类型的FormGroup保障类型安全
forms-typed
8. General Performance (LOW-MEDIUM)
8. 通用性能(中低)
- - Prevent memory leaks (timers, listeners, subscriptions)
perf-memory-leaks - - Offload heavy computation to Web Workers
perf-web-workers - - Use Smart/Dumb component pattern
arch-smart-dumb-components
- - 预防内存泄漏(定时器、监听器、订阅)
perf-memory-leaks - - 将重负载计算任务转移到Web Workers
perf-web-workers - - 使用聪明/木偶组件模式
arch-smart-dumb-components
How to Use
使用方法
Read individual rule files for detailed explanations and code examples:
rules/change-rxjs-state.md
rules/bundle-ngmodule.md
rules/rxjs-takeuntil.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/change-rxjs-state.md
rules/bundle-ngmodule.md
rules/rxjs-takeuntil.md每个规则文件包含:
- 规则重要性的简要说明
- 错误代码示例及解释
- 正确代码示例及解释
- 额外上下文和参考资料
Full Compiled Document
完整编译文档
For the complete guide with all rules expanded:
AGENTS.md如需包含所有展开规则的完整指南,请查看:
AGENTS.md