Angular 使用心得

一、项目结构

src/app/
  ├── core/          # 单例服务、拦截器
  ├── shared/        # 共享组件/管道/指令
  ├── features/      # 业务模块
  └── app.config.ts  # 应用配置

二、状态管理

  • 简单:Signals + service
  • 中等:ComponentStore
  • 复杂:NgRx

三、性能

  • OnPush 变更检测
  • trackBy in *ngFor
  • 懒加载路由
  • 预加载策略

四、测试

  • 单元:Jasmine + Karma / Jest
  • E2E:Cypress / Playwright
  • TestBed 配置 mock

五、常见坑

  • Zone.js 与第三方库冲突 → Zoneless
  • 循环依赖 → 重构
  • 内存泄漏 → takeUntil / async pipe

六、小结

Angular 适合大型团队,规范先行、长期收益大。

本篇为《Angular 前端开发》系列第 4 篇。