组件应用开发
- 作者仓库星标 100,171
- 许可证 MIT
- 作者仓库 angular
- 领域
- 工程开发
- 作者声明的智能体
- 未找到明确声明;不据此推断已兼容或已测试
- 静态检查
- 94 / 100 · 启发式扫描,不代表运行安全
- 运行环境证据
- 未执行;当前没有隔离运行测试证据
- 作者 / 版本 / 许可证
- @angular · MIT
- 上下文用量评级
- 低消耗
- 接入复杂程度
- 需简单配置
- 是否需要外部接口密钥
- 不需要
- 兼容的系统
- macOS · Linux · Windows
- 运行环境要求
- 未检出明确要求
- 检测到的文件与系统行为
-
- 只读
- 允许写入 / 修改
- 命令执行
- 网络行为
- 允许外网请求
- 安装命令数
- 无(仅作为资料)
需要注意: 未声明工具权限范围;实际权限由运行环境决定。
# Angular Developer Guidelines
1. Always analyze the project's Angular version before providing guidance, as best practices and available features can vary significantly between versions. If creating a new project with Angular CLI, do not specify a version unless prompted by the user.
2. When generating code, follow Angular's style guide and best practices for maintainability and performance. Use the Angular CLI for scaffolding components, services, directives, pipes, and routes to ensure consistency.
3. Once you finish generating code, run `ng build` to ensure there are no build errors. If there are errors, analyze the error messages and fix them before proceeding. Do not skip this step, as it is critical for ensuring the generated code is correct and functional.
## Creating New Projects
If no guidelines are provided by the user, here are same default rules to follow when creating a new Angular project:
1. Use the latest stable version of Angular unless the user specifies otherwise.
2. Use Signals Forms for form management in new projects (available in Angular v21 and newer) [Find out more](references/signal-forms.md).
**Execution Rules for `ng new`:**
When asked to create a new Angular project, you must determine the correct execution command by following these strict steps:
**Step 1: Check for an explicit user version.**
- **IF** the user requests a specific version (e.g., Angular 15), bypass local installations and strictly use `npx`.
- **Command:** `npx @angular/cli@<requested_version> new <project-name>`
**Step 2: Check for an existing Angular installation.**
- **IF** no specific version is requested, run `ng version` in the terminal to check if the Angular CLI is already installed on the system.
- **IF** the command succeeds and returns an installed version, use the local/global installation directly.
- **Command:** `ng new <project-name>`
**Step 3: Fallback to Latest.**
- **IF** no specific version is requested AND the `ng version` command fails (indicating no Angular installation exists), you must use `npx` to fetch the latest version.
- **Command:** `npx @angular/cli@latest new <project-name>`
## Components
When working with Angular components, consult the following references based on the task:
- **Fundamentals**: Anatomy, metadata, core concepts, and template control flow (@if, @for, @switch). Read [components.md](references/components.md)
- **Inputs**: Signal-based inputs, transforms, and model inputs. Read [inputs.md](references/inputs.md)
- **Outputs**: Signal-based outputs and custom event best practices. Read [outputs.md](references/outputs.md)
- **Host Elements**: Host bindings and attribute injection. Read [host-elements.md](references/host-elements.md)
If you require deeper documentation not found in the references above, read the documentation at `https://angular.dev/guide/components`.
## Reactivity and Data Management
When managing state and data reactivity, use Angular Signals and consult the following references:
- **Signals Overview**: Core signal concepts (`signal`, `computed`), reactive contexts, and `untracked`. Read [signals-overview.md](references/signals-overview.md)
- **Dependent State (`linkedSignal`)**: Creating writable state linked to source signals. Read [linked-signal.md](references/linked-signal.md)
- **Async Reactivity (`resource`)**: Fetching asynchronous data directly into signal state. Read [resource.md](references/resource.md)
- **Side Effects (`effect`)**: Logging, third-party DOM manipulation (`afterRenderEffect`), and when NOT to use effects. Read [effects.md](references/effects.md)
## Forms
In most cases for new apps, **prefer signal forms**. When making a forms decision, analyze the project and consider the following guidelines:
- if the application is using v21 or newer and this is a new form, **prefer signal forms**.
-For older applications or when working with existing forms, use the appropriate form type that matches the applications current form strategy.
- **Signal Forms**: Use signals for form state management. Read [signal-forms.md](references/signal-forms.md)
- **Template-driven forms**: Use for simple forms. Read [template-driven-forms.md](references/template-driven-forms.md)
- **Reactive forms**: Use for complex forms. Read [reactive-forms.md](references/reactive-forms.md)
## Dependency Injection
When implementing dependency injection in Angular, follow these guidelines:
- **Fundamentals**: Overview of Dependency Injection, services, and the `inject()` function. Read [di-fundamentals.md](references/di-fundamentals.md)
- **Creating and Using Services**: Creating services, the `providedIn: 'root'` option, and injecting into components or other services. Read [creating-services.md](references/creating-services.md)
- **Defining Dependency Providers**: Automatic vs manual provision, `InjectionToken`, `useClass`, `useValue`, `useFactory`, and scopes. Read [defining-providers.md](references/defining-providers.md)
- **Injection Context**: Where `inject()` is allowed, `runInInjectionContext`, and `assertInInjectionContext`. Read [injection-context.md](references/injection-context.md)
- **Hierarchical Injectors**: The `EnvironmentInjector` vs `ElementInjector`, resolution rules, modifiers (`optional`, `skipSelf`), and `providers` vs `viewProviders`. Read [hierarchical-injectors.md](references/hierarchical-injectors.md)
## Angular Aria
When building accessible custom components for any of the following patterns: Accordion, Listbox, Combobox, Menu, Tabs, Toolbar, Tree, Grid, consult the following reference:
- **Angular Aria Components**: Building headless, accessible components (Accordion, Listbox, Combobox, Menu, Tabs, Toolbar, Tree, Grid) and styling ARIA attributes. Read [angular-aria.md](references/angular-aria.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](references/define-routes.md)
- **Route Loading Strategies**: Eager vs lazy loading, and context-aware loading. Read [loading-strategies.md](references/loading-strategies.md)
- **Show Routes with Outlets**: Using `<router-outlet>`, nested outlets, and named outlets. Read [show-routes-with-outlets.md](references/show-routes-with-outlets.md)
- **Navigate to Routes**: Declarative navigation with `RouterLink` and programmatic navigation with `Router`. Read [navigate-to-routes.md](references/navigate-to-routes.md)
- **Control Route Access with Guards**: Implementing `CanActivate`, `CanMatch`, and other guards for security. Read [route-guards.md](references/route-guards.md)
- **Data Resolvers**: Pre-fetching data before route activation with `ResolveFn`. Read [data-resolvers.md](references/data-resolvers.md)
- **Router Lifecycle and Events**: Chronological order of navigation events and debugging. Read [router-lifecycle.md](references/router-lifecycle.md)
- **Rendering Strategies**: CSR, SSG (Prerendering), and SSR with hydration. Read [rendering-strategies.md](references/rendering-strategies.md)
- **Route Transition Animations**: Enabling and customizing the View Transitions API. Read [route-animations.md](references/route-animations.md)
If you require deeper documentation or more context, visit the [official Angular Routing guide](https://angular.dev/guide/routing).
## 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](references/tailwind-css.md)
- **Angular Animations**: Using native CSS (recommended) or the legacy DSL for dynamic effects. Read [angular-animations.md](references/angular-animations.md)
- **Styling components**: Best practices for component styles and encapsulation. Read [component-styling.md](references/component-styling.md)
## Testing
When writing or updating tests, consult the following references based on the task:
- **Fundamentals**: Best practices for unit testing (Vitest), async patterns, and `TestBed`. Read [testing-fundamentals.md](references/testing-fundamentals.md)
- **Component Harnesses**: Standard patterns for robust component interaction. Read [component-harnesses.md](references/component-harnesses.md)
- **Router Testing**: Using `RouterTestingHarness` for reliable navigation tests. Read [router-testing.md](references/router-testing.md)
- **End-to-End (E2E) Testing**: Best practices for E2E tests with Cypress. Read [e2e-testing.md](references/e2e-testing.md)
## Tooling
When working with Angular tooling, consult the following references:
- **Angular CLI**: Creating applications, generating code (components, routes, services), serving, and building. Read [cli.md](references/cli.md)
- **Code Modernization**: Automatically refactoring to modern standards using migrations. Read [migrations.md](references/migrations.md)
- **Angular MCP Server**: Available tools, configuration, and experimental features. Read [mcp.md](references/mcp.md)
章节与文件索引
按说明文件中的顺序排列。
创建新项目
组件
反应性和数据管理
表格
依赖注入
文件/命令 -> ng build · ng new · npx · npx @angular/cli@<requested_version> new <project-name> · ng version · ng new <project-name> · npx @angular/cli@latest new <project-name> · https://angular.dev/guide/components
内容校验值 -> 3ae76e7f92c4
当前原文的自动摘译,尚未人工审校。
创建新项目
如果用户没有提供指导,则在创建新的 Angular 项目时要遵循以下相同的默认规则: 除非用户另有指定,否则使用 Angular 的最新稳定版本。在新项目中使用 Signals Forms 进行表单管理(在 Angular v21 及更高版本中提供)了解更多信息。
ng new的执行规则:当要求创建新的 Angular 项目时,您必须按照以下严格步骤确定正确的执行命令:组件
使用 Angular 组件时,请根据任务查阅以下参考资料: 基础知识:剖析、元数据、核心概念和模板控制流(@if、@for、@switch)。读取 Components.md 输入:基于信号的输入、转换和模型输入。阅读 input.md 输出:基于信号的输出和自定义事件最佳实践。阅读outputs.md 主机元素:主机绑定和属性注入。阅读主机元素.md
反应性和数据管理
管理状态和数据反应性时,请使用 Angular 信号并查阅以下参考资料:信号概述:核心信号概念(
signal、computed)、反应式上下文和untracked。阅读 Signals-overview.md 依赖状态 (linkedSignal):创建链接到源信号的可写状态。阅读 linked-signal.md 异步反应性 (resource):将异步数据直接提取到信号状态中。读取资源.md表格
在大多数情况下,对于新应用程序,更喜欢信号形式。在做出表单决策时,分析项目并考虑以下准则:如果应用程序使用 v21 或更高版本并且这是新表单,则首选信号表单。 - 对于较旧的应用程序或使用现有表单时,请使用与应用程序当前表单策略相匹配的适当表单类型。信号表单:使用信号进行表单状态管理。阅读signal-forms.md
依赖注入
在 Angular 中实现依赖注入时,请遵循以下准则: 基础知识:依赖注入、服务和
inject()函数概述。阅读 di-fundamentals.md 创建和使用服务:创建服务、providedIn: 'root'选项,以及注入组件或其他服务。阅读create-services.md 定义依赖项提供程序:自动与手动配置、InjectionToken、useClass、useValue、useFactory和范围。阅读定义提供者.md