【问题标题】:ng build throws error: "Tried to find bootstrap code, but could not."ng build 抛出错误:“试图找到引导代码,但找不到。”
【发布时间】:2018-07-29 23:33:37
【问题描述】:

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

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

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

import { NgModule } from '@angular/core';
import { LoginComponent } from './login.component';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'
import { HttpClientModule } from '@angular/common/http';
import { LoginService } from '../login.service'


@NgModule({
    declarations: [
        LoginComponent
    ],
    imports: [
        BrowserModule,
        FormsModule,
        HttpClientModule
    ],
    providers: [LoginService],
    bootstrap: [LoginComponent]
})
export class 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

【问题讨论】:

    标签: angular angular-cli ng-build


    【解决方案1】:

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

    第 1 步: 在与 main.ts 文件相同的文件夹中创建一个名为 AppModule.ts 的新文件。

    第 2 步: 将所有内容从 ma​​in.ts 文件移动到 AppModule.ts 文件。

    第 3 步: 移动后,ma​​in.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,它现在编译和构建没有错误引导模块错误。

    【讨论】:

    • 这对我有用,它在日志中有一些错误,但我想这是因为某些模块没有更新。非常感谢。
    • 谢谢。初学者绝对需要这个!
    【解决方案2】:

    以防万一有人遇到同样的问题,我的问题是我在包含要引导的模块的同一文件中引导应用程序(即AppModule),而ng build 需要该模块被导入到你调用bootstrapModule()的文件中。相关代码位于node_modules\@ngtools\webpack\src\entry_resolver.js 中的函数resolveEntryModuleFromMain() 中,该函数查找文件中声明的所有导入,其中调用resolveEntryModuleFromMain() 以引导模块。将对bootstrapModule() 的调用移动到另一个文件并导入AppModule 解决了这个问题(无论如何都是更好的做法)。

    【讨论】:

    • 导入到底是什么意思?我有这个确切的问题,我找不到解决方法。您可以通过一些代码示例扩展您的答案吗?我试图将platformBrowserDynamic().bootstrapModule(SomeModule); 移动到模块自己的ngOnInit() 中,然后将模块导入app.module.ts 中,但我得到了同样的错误。不太清楚我为什么会遇到这种情况。
    猜你喜欢
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 2018-11-18
    相关资源
    最近更新 更多