用途:该技能用于在 Angular 应用中使用独立组件(Standalone Components),以及处理 imports 数组、应用引导和从 NgModule 迁移的场景。资料标注版本为 Angular 21(2025),标签包括 Standalone、Components、Imports、Bootstrap。
主要能力与要点:
- 独立组件:组件使用 standalone: true,并通过 imports 数组声明自身依赖,例如 CommonModule、RouterModule、MatCardModule 或其它组件。
- 引导方式:在 main.ts 中使用 bootstrapApplication(AppComponent, appConfig) 引导独立组件,并可通过 .catch(err => console.error(err)) 处理错误。
- 函数式提供者:应用配置使用 ApplicationConfig 与 providers,例如 provideRouter(routes)、provideHttpClient()、provideAnimations(),以及 provideZoneChangeDetection({ eventCoalescing: true })。
- 路由:使用 RouterOutlet 时加入 imports;懒加载独立组件可用 loadComponent: () => import('./admin/admin.component').then(m => m.AdminComponent)。
- 循环依赖:可用 forwardRef(() => OtherComponent) 处理组件循环导入。
- 迁移:执行 ng generate @angular/core:standalone 进行整体迁移,或使用 ng generate @angular/core:standalone --path=path/to/component 对指定组件迁移。
版本变化:自 Angular 17 起新组件默认独立;推荐用函数式提供者替代 RouterModule;NgModule 已标记为弃用,建议迁移到独立组件。
适用场景:需要以独立组件组织 Angular 应用、替换 NgModule、配置路由与依赖注入、以及执行存量代码迁移的项目。
限制:资料未提供具体收费、价格、平台兼容范围、性能数字或完整使用步骤,这些信息暂未提供;实际使用需结合自身项目与官方文档确认。