Angular ng生成抛出错误:“0”;试图查找引导代码,但未能找到。”;

Angular ng生成抛出错误:“0”;试图查找引导代码,但未能找到。”;,angular,angular-cli,ng-build,Angular,Angular Cli,Ng Build,尝试使用ng build构建我的应用程序时,出现以下错误: 试图找到引导代码,但未能找到。指定可静态分析的引导代码或将entryModule传递给插件选项 我在.angular cli.json中指定的主文件非常简单: 从'@angular/core'导入{NgModule}; 从“./login.component”导入{LoginComponent}; 从“@angular/platform browser”导入{BrowserModule}; 从'@angular/forms'导入{For

尝试使用
ng build
构建我的应用程序时,出现以下错误:

试图找到引导代码,但未能找到。指定可静态分析的引导代码或将entryModule传递给插件选项

我在
.angular cli.json
中指定的主文件非常简单:

从'@angular/core'导入{NgModule};
从“./login.component”导入{LoginComponent};
从“@angular/platform browser”导入{BrowserModule};
从'@angular/forms'导入{FormsModule};
从“@angular/platform browser dynamic”导入{PlatformBrowser dynamic}
从'@angular/common/http'导入{HttpClientModule};
从“../login.service”导入{LoginService}
@NGD模块({
声明:[
登录组件
],
进口:[
浏览器模块,
FormsModule,
HttpClientModule
],
提供者:[登录服务],
引导:[登录组件]
})
导出类AppModule{}
platformBrowserDynamic().bootstrapModule(AppModule);
ng--version
返回以下内容:

Angular CLI: 1.7.0
Node: 6.11.0
OS: win32 x64
Angular: 5.2.5
... common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic

@angular/cli: 1.7.0
@angular-devkit/build-optimizer: 0.3.1
@angular-devkit/core: 0.3.1
@angular-devkit/schematics: 0.3.1
@ngtools/json-schema: 1.2.0
@ngtools/webpack: 1.10.0
@schematics/angular: 0.3.1
@schematics/package-update: 0.3.1
typescript: 2.7.2
webpack-replace: 1.0.0
webpack-vendor-chunk-plugin: 1.0.0
webpack: 3.11.0

万一有人遇到同样的问题,我的问题是,我正在将应用程序引导到包含要引导的模块的同一文件中(即
AppModule
),而
ng build
希望将模块导入到调用
bootstrapModule()
的文件中。相关代码位于
node\u modules\@ngtools\webpack\src\entry\u resolver.js
中的函数
resolventrymodulefrommain()
,该函数查找文件中声明的所有导入,其中调用
resolventrymodulefrommain()
以引导模块。将对
bootstrapModule()
的调用移动到另一个文件并导入
AppModule
修复了这个问题(无论如何,这是一个更好的做法)

从大卫的回答中得到一个线索,我尝试了这个,并为我工作

步骤1: 在main.ts文件所在的文件夹中创建一个名为AppModule.ts的新文件

步骤2: 将所有内容从main.ts文件移动到AppModule.ts文件

步骤3: 移动后,main.ts文件应仅包含以下代码:

import './polyfills';

import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {AppModule} from './AppModule';

platformBrowserDynamic().bootstrapModule(AppModule);
步骤4: 我的AppModule.ts文件包含以下代码。您的AppModule.ts可能有不同的代码,具体取决于您所编写的代码

import {HttpClientModule} from '@angular/common/http';
import {NgModule} from '@angular/core';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {BrowserModule} from '@angular/platform-browser';
import {YOURCustomCode} from './app/YOURCustomCode';

@NgModule({
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    FormsModule,
    HttpClientModule,
    DemoMaterialModule,
    MatNativeDateModule,
    ReactiveFormsModule,
  ],
  entryComponents: [YOURCustomCode],
  declarations: [YOURCustomCode],
  bootstrap: [YOURCustomCode],
  providers: []
})
export class AppModule {}
步骤5
运行命令
ng serve
,编译和生成时不会出现错误引导模块错误。

导入的确切含义是什么?我有这个问题,我找不到解决办法。你能用一些代码示例来扩展你的答案吗?我试图移动
平台浏览器动态().bootstrapModule(SomeModule)
导入模块自己的
ngOnInit()
,然后导入
app.module.ts
中的模块,但我得到了相同的错误。我不太清楚我为什么会遇到这种情况。这对我来说很有效,日志中有一些错误,但我想这是因为一些模块没有更新。非常感谢,谢谢。初学者肯定需要这个!