问题描述
尝试使用ng-build
构建我的应用程序时,出现以下错误:
When trying to build my app with ng-build
, I get the following error:
.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
推荐答案
从戴维的答案中得到了一个线索,我尝试了这个并为我工作.
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步:将所有内容从 main.ts 文件移动到 AppModule.ts 文件.
Step 2:Move everything from main.ts file to AppModule.ts file.
第3步:移动之后, main.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引发错误:“试图找到引导程序代码,但找不到."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!