angular-dev-v19
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAngular Developer Guidelines — v19
Angular开发者指南 — v19
This skill targets existing Angular v19 projects. All guidance, APIs, and examples are written for v19. Features exclusive to v20+ or v21+ are not included.
**本技能面向现有Angular v19项目。**所有指导、API和示例均针对v19编写,不包含v20+或v21+专属的功能。
Legacy Syntax Policy (STRICT — applies to all code generation)
遗留语法策略(严格执行 — 适用于所有代码生成)
The following legacy Angular APIs are FORBIDDEN in new code. Never generate them. Never suggest them. Always use the modern equivalent.
| Legacy (NEVER use) | Modern (ALWAYS use) | Notes |
|---|---|---|
| | Signal-based, reactive, type-safe |
| | No |
| | Declarative, co-located |
| | Declarative, co-located |
| | Signal-based queries |
| | Signal-based queries |
| | Built-in control flow |
| | Functional injection |
| Standalone components | |
| | Reactive by default |
| | Prefer modern alternatives; |
以下Angular遗留API在新代码中禁止使用。绝不要生成或推荐它们,务必使用现代替代方案。
| 遗留语法(禁止使用) | 现代替代方案(必须使用) | 说明 |
|---|---|---|
| | 基于Signal、响应式、类型安全 |
| | 无需导入 |
| 元数据中的 | 声明式、内联定义 |
| 元数据中的 | 声明式、内联定义 |
| | 基于Signal的查询 |
| | 基于Signal的查询 |
| | 内置控制流 |
| | 函数式注入 |
| 独立组件 | v19中 |
用于Signal派生的 | | 默认响应式 |
用于清理的 | | 优先使用现代替代方案; |
Refactoring Guidance
重构指导
When the agent is editing or working in a file that uses legacy syntax:
- Complete the user's requested task first using modern syntax only.
- After the task is done, suggest a follow-up refactoring opportunity:
"This file uses legacy/
@Input()decorators. Want me to refactor them to signal-based@Output()/input()as a separate change?"output() - Never mix — if you add new inputs/outputs to a file with legacy ones, still use the modern API for the new additions. Do not match the file's legacy style.
- Never refactor in the same change unless the user explicitly asks for it.
当Agent在使用遗留语法的文件中编辑或工作时:
- 首先完成用户请求的任务,且仅使用现代语法。
- 任务完成后,建议后续的重构机会:
"此文件使用了遗留的/
@Input()装饰器。是否需要我将它们重构为基于Signal的@Output()/input(),作为单独的变更?"output() - 绝不混用 — 若向使用遗留语法的文件添加新的输入/输出,仍需为新增内容使用现代API,不要匹配文件的遗留风格。
- 除非用户明确要求,否则不要在同一变更中进行重构。
When Asked to Refactor Legacy Code
当被要求重构遗留代码时
Do not rewrite manually. Angular ships official schematics that perform full-program analysis across TypeScript and templates. Always prefer a schematic over hand-editing.
Protocol:
- Read the affected and
.tsfiles to identify which legacy patterns are present..html - Consult refactoring.md to select the correct schematic.
- Run the schematic (scoped with when appropriate).
--path - Run to verify no errors were introduced.
ng build
Read refactoring.md for all available schematics, exact commands, flags, and known limitations.
不要手动重写。Angular官方提供了schematics,可对TypeScript和模板进行全程序分析。始终优先使用schematics而非手动编辑。
流程:
- 读取受影响的和
.ts文件,识别存在的遗留模式。.html - 查阅refactoring.md选择正确的schematic。
- 运行schematic(必要时使用参数限定范围)。
--path - 运行验证未引入错误。
ng build
查看refactoring.md获取所有可用的schematics、精确命令、参数和已知限制。
General Guidelines
通用指导原则
- When generating code, follow Angular's style guide and best practices for maintainability and performance.
- Once you finish generating code, run to ensure there are no build errors. If there are errors, analyze the error messages and fix them before proceeding.
ng build
- 生成代码时,遵循Angular风格指南和最佳实践,以保证可维护性和性能。
- 代码生成完成后,运行确保无构建错误。若存在错误,分析错误信息并修复后再继续。
ng build
Components
组件
When working with Angular components, consult the following references based on the task:
- Fundamentals: Anatomy, metadata, and core concepts. Read components.md
- Inputs: Signal-based inputs, transforms, and model inputs. Read inputs.md
- Outputs: Signal-based outputs and custom event best practices. Read outputs.md
- Host Elements: Host bindings and attribute injection. Read host-elements.md
- Templates: Control flow, @let, class/style bindings, typed form access. Read templates.md
- Lifecycle: Signals-first lifecycle, DestroyRef, afterNextRender, takeUntilDestroyed. Read lifecycle.md
- Directives: Attribute, host directives, directive composition API, and the same-name alias convention. Read directives.md
- Structural Directives: Portal, lazy render, domain-logic conditionals (role/permission), typed template outlet, and microsyntax reference. Read structural-directives.md
- Refactoring Legacy Code: All Angular migration schematics (,
signal-input-migration,output-migration,signal-queries-migration,inject-migration,control-flow-migration, and more). Read refactoring.mdstandalone-migration
If you require deeper documentation not found in the references above, read the documentation at .
https://angular.dev/guide/components处理Angular组件时,根据任务查阅以下参考文档:
- 基础: 组件结构、元数据和核心概念。阅读components.md
- 输入: 基于Signal的输入、转换和模型输入。阅读inputs.md
- 输出: 基于Signal的输出和自定义事件最佳实践。阅读outputs.md
- 宿主元素: 宿主绑定和属性注入。阅读host-elements.md
- 模板: 控制流、@let、类/样式绑定、类型化表单访问。阅读templates.md
- 生命周期: 优先使用Signal的生命周期、DestroyRef、afterNextRender、takeUntilDestroyed。阅读lifecycle.md
- 指令: 属性指令、宿主指令、指令组合API和同名别名约定。阅读directives.md
- 结构型指令: 门户、延迟渲染、领域逻辑条件(角色/权限)、类型化模板出口和微语法参考。阅读structural-directives.md
- 遗留代码重构: 所有Angular迁移schematics(、
signal-input-migration、output-migration、signal-queries-migration、inject-migration、control-flow-migration等)。阅读refactoring.mdstandalone-migration
若上述参考文档未包含所需的详细内容,请查阅官方文档。
https://angular.dev/guide/componentsReactivity and Data Management
响应式编程与数据管理
When managing state and data reactivity, use Angular Signals and consult the following references:
- Signals Overview: Core signal concepts (,
signal), reactive contexts, andcomputed. Read signals-overview.mduntracked - Dependent State (): Creating writable state linked to source signals. Read linked-signal.md
linkedSignal - Async Reactivity (): Fetching asynchronous data directly into signal state. Read resource.md
resource⚠️,resource(), andhttpResource()are experimental in v19. Use with awareness that the API may change in v20+.rxResource() - Side Effects (): Logging, third-party DOM manipulation, and when NOT to use effects. Read effects.md
effect - RxJS Interop: Bridging signals and observables with ,
toSignal(), andtoObservable(). Read rxjs-interop.mdtakeUntilDestroyed()
管理状态和数据响应式时,使用Angular Signals并查阅以下参考文档:
- Signals概述: Signal核心概念(、
signal)、响应式上下文和computed。阅读signals-overview.mduntracked - 依赖状态(): 创建与源Signal关联的可写状态。阅读linked-signal.md
linkedSignal - 异步响应式(): 直接将异步数据获取到Signal状态中。阅读resource.md
resource⚠️ v19中、resource()和httpResource()为实验性API。使用时请注意,该API可能在v20+中发生变更。rxResource() - 副作用(): 日志记录、第三方DOM操作以及副作用的禁用场景。阅读effects.md
effect - RxJS互操作: 使用、
toSignal()和toObservable()实现Signal与Observable的桥接。阅读rxjs-interop.mdtakeUntilDestroyed()
Forms
表单
Default for v19: Typed Reactive Forms.
- Reactive forms: Use for all forms with typed FormGroup/FormControl. Read reactive-forms.md
Signal Forms () are not available in v19. Do not reference them.@angular/forms/signals
v19默认:类型化响应式表单。
- 响应式表单: 所有表单均使用类型化FormGroup/FormControl。阅读reactive-forms.md
v19中不支持Signal Forms(),请勿引用。@angular/forms/signals
Dependency Injection
依赖注入
When implementing dependency injection in Angular, follow these guidelines:
- Fundamentals: Overview of Dependency Injection, services, and the function. Read di-fundamentals.md
inject() - Creating and Using Services: Creating services, the option, and injecting into components or other services. Read creating-services.md
providedIn: 'root' - Defining Dependency Providers: Automatic vs manual provision, ,
InjectionToken,useClass,useValue, and scopes. Read defining-providers.mduseFactory - Injection Context: Where is allowed,
inject(), andrunInInjectionContext. Read injection-context.mdassertInInjectionContext - Hierarchical Injectors: The vs
EnvironmentInjector, resolution rules, modifiers (ElementInjector,optional), andskipSelfvsproviders. Read hierarchical-injectors.mdviewProviders - Services: Service design patterns, single responsibility, and facade pattern. Read services.md
在Angular中实现依赖注入时,请遵循以下指导原则:
- 基础: 依赖注入概述、服务和函数。阅读di-fundamentals.md
inject() - 服务创建与使用: 创建服务、选项以及向组件或其他服务注入。阅读creating-services.md
providedIn: 'root' - 依赖提供者定义: 自动与手动提供、、
InjectionToken、useClass、useValue和作用域。阅读defining-providers.mduseFactory - 注入上下文: 的允许使用场景、
inject()和runInInjectionContext。阅读injection-context.mdassertInInjectionContext - 分层注入器: 与
EnvironmentInjector的对比、解析规则、修饰符(ElementInjector、optional)以及skipSelf与providers的区别。阅读hierarchical-injectors.mdviewProviders - 服务: 服务设计模式、单一职责原则和外观模式。阅读services.md
Routing
路由
When implementing navigation in Angular, consult the following references:
- Define Routes: URL paths, static vs dynamic segments, wildcards, and redirects. Read define-routes.md
- Show Routes with Outlets: Using , nested outlets, and named outlets. Read show-routes-with-outlets.md
<router-outlet> - Navigate to Routes: Declarative navigation with and programmatic navigation with
RouterLink. Read navigate-to-routes.mdRouter - Control Route Access with Guards: Implementing ,
CanActivate, and other guards for security. Read route-guards.mdCanMatch - Data Resolvers: Pre-fetching data before route activation with . Read data-resolvers.md
ResolveFn - Router Lifecycle and Events: Chronological order of navigation events and debugging. Read router-lifecycle.md
If you require deeper documentation or more context, visit the official Angular Routing guide.
在Angular中实现导航时,查阅以下参考文档:
- 定义路由: URL路径、静态与动态分段、通配符和重定向。阅读define-routes.md
- 使用路由出口展示路由: 使用、嵌套出口和命名出口。阅读show-routes-with-outlets.md
<router-outlet> - 导航至路由: 使用的声明式导航和使用
RouterLink的编程式导航。阅读navigate-to-routes.mdRouter - 使用守卫控制路由访问: 实现、
CanActivate等守卫以保障安全。阅读route-guards.mdCanMatch - 数据解析器: 使用在路由激活前预获取数据。阅读data-resolvers.md
ResolveFn - 路由生命周期与事件: 导航事件的时间顺序和调试方法。阅读router-lifecycle.md
若需要更详细的文档或更多上下文,请访问官方Angular路由指南。
HTTP & Data Fetching
HTTP与数据获取
When implementing HTTP and data fetching, consult the following references:
- HTTP entry point (choosing the right API, mutations, error handling): Read http.md
HttpClient - Resource APIs (,
httpResource(),resource(), state signals, loading states): Read resource.mdrxResource()⚠️ All resource APIs are experimental in v19. - Interceptors (functional interceptors, auth, error, registration): Read interceptors.md
实现HTTP和数据获取时,查阅以下参考文档:
- HTTP入口(选择合适的API、变更、错误处理):阅读http.md
HttpClient - Resource API(、
httpResource()、resource()、状态Signal、加载状态):阅读resource.mdrxResource()⚠️ v19中所有Resource API均为实验性。 - 拦截器(函数式拦截器、认证、错误处理、注册):阅读interceptors.md
Styling and Animations
样式与动画
When implementing styling and animations in Angular, consult the following references:
- Using Tailwind CSS with Angular: Integrating Tailwind CSS into Angular projects. Read tailwind-css.md
- Angular Animations: Use native CSS animations (recommended) or the DSL for dynamic effects. Read angular-animations.md
@angular/animations/animate.enterare not available in v19. Use CSS animations or theanimate.leaveDSL.@angular/animations - Styling components: Best practices for component styles and encapsulation. Read component-styling.md
在Angular中实现样式与动画时,查阅以下参考文档:
- Angular集成Tailwind CSS: 将Tailwind CSS集成到Angular项目中。阅读tailwind-css.md
- Angular动画: 推荐使用原生CSS动画,或使用DSL实现动态效果。阅读angular-animations.md
@angular/animationsv19中不支持/animate.enter。请使用CSS动画或animate.leaveDSL。@angular/animations - 组件样式: 组件样式的最佳实践与封装。阅读component-styling.md
Testing
测试
When writing or updating tests, consult the following references based on the task:
- Fundamentals: Best practices for unit testing, async patterns, and . Use Jasmine/Karma or a manual Vitest setup (native Vitest support is v20+ only). Read testing-fundamentals.md
TestBed - Component Harnesses: Standard patterns for robust component interaction. Read component-harnesses.md
- Router Testing: Using for reliable navigation tests. Read router-testing.md
RouterTestingHarness
编写或更新测试时,根据任务查阅以下参考文档:
- 基础: 单元测试最佳实践、异步模式和。使用Jasmine/Karma或手动配置Vitest(原生Vitest支持仅在v20+中提供)。阅读testing-fundamentals.md
TestBed - 组件测试工具: 用于可靠组件交互的标准模式。阅读component-harnesses.md
- 路由测试: 使用进行可靠的导航测试。阅读router-testing.md
RouterTestingHarness
Tooling
工具链
When working with Angular tooling, consult the following references:
- Nx: This project uses Nx. Use ,
nx generate,nx build, andnx serveinstead of the Angular CLI directly. Refer to the Nx docs for available generators and executors.nx test
使用Angular工具链时,查阅以下参考文档:
- Nx: 本项目使用Nx。请使用、
nx generate、nx build和nx serve替代Angular CLI直接执行。参考Nx文档获取可用的生成器和执行器。nx test