You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular CLI 17.0.10执行ng new未生成src根目录下的app.module.ts

Angular 17 无app.module.ts、新增app.config.ts的原因及指引

是的,这是Angular 17默认启用的独立组件模式带来的变化,完全替代了传统NgModule(app.module.ts)的注册逻辑。

1. 为什么没有app.module.ts?

  • Angular 17起,CLI创建新项目默认采用独立组件模式:组件通过standalone: true标记即可独立运行,无需依赖NgModule来声明、导入或导出组件/服务。
  • 传统NgModule模式并未被移除,只是不再作为默认选项存在。

2. app.config.ts的核心作用

它是独立模式下的全局配置入口,用来管理应用级的依赖注入、路由、全局服务等,替代了原app.module.ts中imports、providers等配置项:

  • 示例配置(注入HttpClient):
    import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
    import { provideRouter } from '@angular/router';
    import { provideHttpClient } from '@angular/common/http';
    
    import { routes } from './app.routes';
    
    export const appConfig: ApplicationConfig = {
      providers: [
        provideZoneChangeDetection({ eventCoalescing: true }),
        provideRouter(routes),
        provideHttpClient() // 替代原NgModule中导入HttpClientModule的操作
      ]
    };
    
  • 应用启动时,main.ts会通过bootstrapApplication(AppComponent, appConfig)加载这个配置,完成初始化。

3. 如何回到传统NgModule模式(生成app.module.ts)

如果习惯使用旧模式,创建项目时添加--no-standalone参数即可:

ng new my-project --no-standalone

执行该命令后,CLI会生成熟悉的app.module.ts文件,应用启动逻辑也会回到platformBrowserDynamic().bootstrapModule(AppModule)。

4. 关于官方文档的说明

官方文档提及app.module.ts是因为NgModule模式仍被官方支持,你可以在文档中搜索“standalone components”关键词,找到独立组件模式的完整指引。

内容的提问来源于stack exchange,提问作者MJ X

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 01:07:02