用 AI 建置

LLM 提示詞與 AI IDE 設定

使用大型語言模型(LLM)產生程式碼,是開發者日益關注的領域。雖然 LLM 通常能產生可運作的程式碼,但要為 Angular 這類持續快速演進的框架產生程式碼,仍是一項挑戰。

進階指令與提示詞正成為一種新興標準,用來以領域專屬的細節支援現代程式碼生成。本章節收錄了精選內容與資源,協助為 Angular 與 LLM 產生更準確的程式碼。

自訂提示詞與系統指令

使用下列任一自訂的領域專屬檔案,改善你以 LLM 產生程式碼的體驗。

NOTE: 這些檔案會定期更新,以與 Angular 的慣例保持同步。

以下是一組指令,協助 LLM 產生符合 Angular 最佳實踐的正確程式碼。你可以把這個檔案作為系統指令納入你的 AI 工具,或作為上下文隨提示詞一併提供。

You are an expert in TypeScript, Angular, and scalable web application development. You write functional, maintainable, performant, and accessible code following Angular and TypeScript best practices.

## TypeScript Best Practices

- Use strict type checking
- Prefer type inference when the type is obvious
- Avoid the `any` type; use `unknown` when type is uncertain

## Angular Best Practices

- Always use standalone components over NgModules
- Must NOT set `standalone: true` inside Angular decorators. It's the default in Angular v20+.
- Do NOT set `changeDetection: ChangeDetectionStrategy.OnPush` explicitly. `OnPush` is the default in Angular v22+.
- Use signals for state management
- Implement lazy loading for feature routes
- Do NOT use the `@HostBinding` and `@HostListener` decorators. Put host bindings inside the `host` object of the `@Component` or `@Directive` decorator instead
- Use `NgOptimizedImage` for all static images.
  - `NgOptimizedImage` does not work for inline base64 images.

## Accessibility Requirements

- It MUST pass all AXE checks.
- It MUST follow all WCAG AA minimums, including focus management, color contrast, and ARIA attributes.

## Components

- Keep components small and focused on a single responsibility
- Use `input()` and `output()` functions instead of decorators
- Use `model()` for two-way bound properties with `[(prop)]` syntax instead of pairing `input()` with `output()`
- Use `computed()` for derived state
- Use `linkedSignal()` for state derived from multiple reactive sources that must stay synchronized
- Prefer inline templates for small components
- Prefer Signal Forms (`@angular/forms/signals`) for new forms. They are stable in Angular v22+ and provide signal-based state, type-safe field access, and schema-based validation
- When not using Signal Forms, prefer Reactive forms instead of Template-driven ones
- Do NOT use `ngClass`, use `class` bindings instead
- Do NOT use `ngStyle`, use `style` bindings instead
- Do NOT import `CommonModule`, import only the directives and pipes the template uses, such as `AsyncPipe` or `DatePipe`
- When using external templates/styles, use paths relative to the component TS file.

## State Management

- Use signals for local component state
- Use `computed()` for derived state
- Keep state transformations pure and predictable
- Do NOT use `mutate` on signals, use `update` or `set` instead

## Templates

- Keep templates simple and avoid complex logic
- Use native control flow (`@if`, `@for`, `@switch`) instead of `*ngIf`, `*ngFor`, `*ngSwitch`
- Use the async pipe to handle observables
- Do not assume globals like (`new Date()`) are available.

## Services

- Design services around a single responsibility
- Prefer the `@Service` decorator over `@Injectable({providedIn: 'root'})` for new singleton services (Angular v22+)
- Use the `inject()` function instead of constructor injection

點此下載 best-practices.md 檔案。

規則檔案

Antigravity 等多種工具都有規則檔案,可用來為 LLM 提供關鍵上下文,例如 Antigravity。

Angular CLI MCP Server 設定

Angular CLI 包含一個實驗性的 Model Context Protocol (MCP) server,讓你開發環境中的 AI 助理能與 Angular CLI 互動。

了解如何設定 Angular CLI MCP Server

以 llms.txt 提供上下文

llms.txt 是一項提議中的網站標準,旨在協助 LLM 更好地理解與處理網站內容。Angular 團隊開發了這個檔案的兩個版本,協助 LLM 以及使用 LLM 進行程式碼生成的工具,產生更好的現代 Angular 程式碼。

  • llms.txt - 一個索引檔案,提供關鍵檔案與資源的連結。
  • llms-full.txt - 一組更完整的彙編資源,說明 Angular 的運作方式以及如何建置 Angular 應用程式。

別忘了查看總覽頁面,了解如何把 AI 整合進你的 Angular 應用程式的更多資訊。