gpt4 book ai didi

angular - ng 构建抛出错误 : "Tried to find bootstrap code, but could not."

转载 作者:行者123 更新时间:2023-12-02 17:43:11 26 4
gpt4 key购买 nike

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

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 中指定的我的主文件非常简单:

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

最佳答案

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

第 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 步:运行命令 ngserve ,它会编译并构建,现在没有错误引导模块错误。

关于angular - ng 构建抛出错误 : "Tried to find bootstrap code, but could not.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48871139/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com