angular-cli
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinese@angular/cli
@angular/cli
Version: Angular 21 (2025)
Tags: CLI, Generators, Build, Scaffolding
API Changes
API 变更
This section documents recent version-specific API changes.
-
NEW: Control flow migration —for @if/@for migration source
ng g @angular/core:control-flow -
NEW: Signal input migration —for signal inputs source
ng g @angular/core:signal-input-migration -
NEW: Route lazy loading migration —for standalone routes
ng g @angular/core:route-lazy-loading -
NEW: Named workspaces — Multiple applications in single angular.json
-
NEW: esbuild by default — Faster builds with esbuild/Vite
Best Practices
最佳实践
- Use generators for scaffolding
bash
undefined- 使用生成器搭建代码骨架
bash
undefinedGenerate component
生成组件
ng g c components/my-component
ng g c components/my-component
Generate service
生成服务
ng g s services/my-service
ng g s services/my-service
Generate guard
生成路由守卫
ng g g guards/auth
ng g g guards/auth
Generate interceptor
生成拦截器
ng g interceptor timing
ng g interceptor timing
Generate library
生成库
ng g library my-lib
ng g library my-lib
Generate with standalone
生成独立组件
ng g c my-component --standalone
- Use schematics for migrations
```bashng g c my-component --standalone
- 使用schematics执行迁移
```bashMigrate to control flow
迁移到控制流语法
ng g @angular/core:control-flow
ng g @angular/core:control-flow
Migrate to signal inputs
迁移到信号输入
ng g @angular/core:signal-input-migration
ng g @angular/core:signal-input-migration
Migrate to lazy loading routes
迁移到路由懒加载
ng g @angular/core:route-lazy-loading
- Use ng add for packages
```bashng g @angular/core:route-lazy-loading
- 使用ng add安装依赖包
```bashAdd Angular Material
安装Angular Material
ng add @angular/material
ng add @angular/material
Add SSR
添加SSR支持
ng add @angular/ssr
ng add @angular/ssr
Add PWA
添加PWA支持
ng add @angular/pwa
- Configure workspace defaults
```json
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"my-app": {
"schematics": {
"@schematics/angular:component": {
"style": "scss",
"standalone": true
}
}
}
}
}- Use ng run for builders
bash
undefinedng add @angular/pwa
- 配置工作区默认设置
```json
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"my-app": {
"schematics": {
"@schematics/angular:component": {
"style": "scss",
"standalone": true
}
}
}
}
}- 使用ng run执行构建器
bash
undefinedRun custom builder
运行自定义构建器
ng run my-app:build
ng run my-app:build
Run SSR
运行SSR服务
ng run my-app:serve-ssr
ng run my-app:serve-ssr
Run prerender
执行预渲染
ng run my-app:prerender
- Use ng config for settings
```bashng run my-app:prerender
- 使用ng config修改设置
```bashSet default style
设置默认样式格式
ng config defaults.style=scss
ng config defaults.style=scss
Enable analytics
启用统计分析
ng analytics enable
- Use ng update for migrations
```bashng analytics enable
- 使用ng update执行版本迁移
```bashUpdate packages
更新所有依赖包
ng update
ng update
Update specific package
更新指定依赖包
ng update @angular/core
- Use ng build with options
```bashng update @angular/core
- 搭配参数使用ng build
```bashProduction build
生产环境构建
ng build --configuration=production
ng build --configuration=production
Dev build with stats
生成构建统计文件的开发环境构建
ng build --stats-json
- Use standalone by default
```bashng build --stats-json
- 默认使用独立组件模式
```bashNew project with standalone
创建默认启用独立组件的新项目
ng new my-app --standalone
- Use functional over class-based
```bashng new my-app --standalone
- 优先使用函数式而非类式实现
```bashGenerate functional guard
生成函数式路由守卫
ng g g guards/auth --functional
ng g g guards/auth --functional
Generate functional interceptor
生成函数式拦截器
ng g interceptor my-interceptor --functional
undefinedng g interceptor my-interceptor --functional
undefined