angular-tooling

angular-tooling

热门

在 Angular v20+ 项目中有效使用 Angular CLI 和开发工具。用于项目设置、代码生成、构建、测试和配置。触发条件包括创建新项目、生成组件/服务/模块、配置构建、运行测试或优化生产构建。不适用于 Nx 工作区命令、自定义 Webpack 配置或非 Angular CLI 构建系统(如独立 Vite 或直接使用 esbuild)。

596Star
70Fork
更新于 2026/3/23
SKILL.md
readonly只读
name
angular-tooling
description

在 Angular v20+ 项目中有效使用 Angular CLI 和开发工具。用于项目设置、代码生成、构建、测试和配置。触发条件包括创建新项目、生成组件/服务/模块、配置构建、运行测试或优化生产构建。不适用于 Nx 工作区命令、自定义 Webpack 配置或非 Angular CLI 构建系统(如独立 Vite 或直接使用 esbuild)。

Angular 工具

使用 Angular CLI 和开发工具进行高效的 Angular v20+ 开发。

项目设置

创建新项目

# 创建新的独立项目(v20+ 默认)
ng new my-app

# 带特定选项
ng new my-app --style=scss --routing --ssr=false

# 跳过测试
ng new my-app --skip-tests

# 最小化设置
ng new my-app --minimal --inline-style --inline-template

项目结构

my-app/
├── src/
│   ├── app/
│   │   ├── app.component.ts
│   │   ├── app.config.ts
│   │   └── app.routes.ts
│   ├── index.html
│   ├── main.ts
│   └── styles.scss
├── public/                  # 静态资源
├── angular.json             # CLI 配置
├── package.json
├── tsconfig.json
└── tsconfig.app.json

代码生成

组件

# 生成组件
ng generate component features/user-profile
ng g c features/user-profile  # 简写形式

# 带选项
ng g c shared/button --inline-template --inline-style
ng g c features/dashboard --skip-tests
ng g c features/settings --change-detection=OnPush

# 扁平(无文件夹)
ng g c shared/icon --flat

# 预演(预览)
ng g c features/checkout --dry-run

服务

# 生成服务(默认 providedIn: 'root')
ng g service services/auth
ng g s services/user

# 跳过测试
ng g s services/api --skip-tests

其他原理图

# 指令
ng g directive directives/highlight
ng g d directives/tooltip

# 管道
ng g pipe pipes/truncate
ng g p pipes/date-format

# 守卫(默认函数式)
ng g guard guards/auth

# 拦截器(默认函数式)
ng g interceptor interceptors/auth

# 接口
ng g interface models/user

# 枚举
ng g enum models/status

# 类
ng g class models/product

使用路径别名生成

# 功能文件夹中的组件
ng g c @features/products/product-list
ng g c @shared/ui/button

开发服务器

# 启动开发服务器
ng serve
ng s  # 简写形式

# 带选项
ng serve --port 4201
ng serve --open  # 打开浏览器
ng serve --host 0.0.0.0  # 暴露到网络

# 本地生产模式
ng serve --configuration=production

# 带 SSL
ng serve --ssl --ssl-key ./ssl/key.pem --ssl-cert ./ssl/cert.pem

构建

开发构建

ng build

生产构建

ng build --configuration=production
ng build -c production  # 简写形式

# 带特定选项
ng build -c production --source-map=false
ng build -c production --named-chunks

构建输出

dist/my-app/
├── browser/
│   ├── index.html
│   ├── main-[hash].js
│   ├── polyfills-[hash].js
│   └── styles-[hash].css
└── server/              # 如果启用了 SSR
    └── main.js

测试

单元测试

# 运行测试
ng test
ng t  # 简写形式

# 单次运行(CI)
ng test --watch=false --browsers=ChromeHeadless

# 带覆盖率
ng test --code-coverage

# 特定文件
ng test --include=**/user.service.spec.ts

E2E 测试

# 运行 e2e(如果已配置)
ng e2e

代码检查

# 运行代码检查
ng lint

# 修复可自动修复的问题
ng lint --fix

配置

angular.json 关键部分

{
  "projects": {
    "my-app": {
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:application",
          "options": {
            "outputPath": "dist/my-app",
            "index": "src/index.html",
            "browser": "src/main.ts",
            "polyfills": ["zone.js"],
            "tsConfig": "tsconfig.app.json",
            "assets": ["{ \"glob\": \"**/*\", \"input\": \"public\" }"],
            "styles": ["src/styles.scss"],
            "scripts": []
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kB",
                  "maximumError": "1MB"
                }
              ],
              "outputHashing": "all"
            },
            "development": {
              "optimization": false,
              "extractLicenses": false,
              "sourceMap": true
            }
          }
        }
      }
    }
  }
}

环境配置

// src/environments/environment.ts
export const environment = {
  production: false,
  apiUrl: 'http://localhost:3000/api',
};

// src/environments/environment.prod.ts
export const environment = {
  production: true,
  apiUrl: 'https://api.example.com',
};

在 angular.json 中配置:

{
  "configurations": {
    "production": {
      "fileReplacements": [
        {
          "replace": "src/environments/environment.ts",
          "with": "src/environments/environment.prod.ts"
        }
      ]
    }
  }
}

添加库

Angular 库

# 添加 Angular Material
ng add @angular/material

# 添加 Angular PWA
ng add @angular/pwa

# 添加 Angular SSR
ng add @angular/ssr

# 添加 Angular Localize
ng add @angular/localize

第三方库

# 安装并配置
npm install @ngrx/signals

# 某些库有原理图
ng add @ngrx/store

更新 Angular

# 检查更新
ng update

# 更新 Angular 核心和 CLI
ng update @angular/core @angular/cli

# 更新所有包
ng update --all

# 强制更新(跳过依赖检查)
ng update @angular/core @angular/cli --force

性能分析

# 构建并生成统计信息
ng build -c production --stats-json

# 分析包(安装 esbuild-visualizer)
npx esbuild-visualizer --metadata dist/my-app/browser/stats.json --open

缓存

# 启用持久构建缓存(v20+ 默认)
# 在 angular.json 中配置:
{
  "cli": {
    "cache": {
      "enabled": true,
      "path": ".angular/cache",
      "environment": "all"
    }
  }
}

# 清除缓存
rm -rf .angular/cache

有关高级配置,请参阅 references/tooling-patterns.md