ng build 抛出错误:“试图找到引导代码,但找不到." [英] ng build throws error: "Tried to find bootstrap code, but could not."

查看:21
本文介绍了ng build 抛出错误:“试图找到引导代码,但找不到."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When trying to build my app with ng-build, I get the following error:

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

Tried to find bootstrap code, but could not. Specify either statically analyzable bootstrap code or pass in an entryModule to the plugins options.

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

My main file as specified in .angular-cli.json is pretty straightforward:

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

推荐答案

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

Taking a clue from David's answer, I tried this and worked for me.

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

Step 1: Create a new file named AppModule.ts inside the same folder as you have main.ts file.

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

Step 2: Move everything from main.ts file to AppModule.ts file.

第 3 步:移动后,ma​​in.ts 文件应该只有以下代码:

Step 3: After the move, main.ts file should ONLY have this code:

import './polyfills';

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

platformBrowserDynamic().bootstrapModule(AppModule);

第 4 步:我的 AppModule.ts 文件有这个代码.根据您编码的内容,您的 AppModule.ts 可能具有不同的代码.

Step 4: My AppModule.ts file has this code. Your AppModule.ts may have different code based on what you have coded.

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,它现在编译和构建没有错误引导模块错误.

Step 5: Run command ng serve and it compiles and builds with no error bootstrap module error now.

这篇关于ng build 抛出错误:“试图找到引导代码,但找不到."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆