gpt4 book ai didi

Angular2,AoT编译: Cannot determine the module for component AppComponent

转载 作者:太空狗 更新时间:2023-10-29 17:10:15 25 4
gpt4 key购买 nike

总结:我正在尝试将 Angular2 AoT 用于我的 Angular2 应用程序,但由于我有静态提供程序将一些值从服务器传递到 Angular2,ngc 显示了一些错误.我的问题是如何获取使用 ngc 创建的 ngFactory 文件。

详细信息:我正在使用 cookbook guide ,然后运行 ​​"node_modules/.bin/ngc"-p tsconfig-aot.json 显示以下错误:

Error: Cannot determine the module for component AppComponent!
at F:\workspace\node\my-project\node_modules\@angular\compiler\bundles\compiler.umd.js:12839:25
at Array.map (native)
at OfflineCompiler.compile (F:\workspace\node\my-project\node_modules\@angular\compiler\bundles\compiler.umd.js:12835:31)
at F:\workspace\node\my-project\node_modules\@angular\compiler-cli\src\codegen.js:108:18
at Array.map (native)
at CodeGenerator.codegen (F:\workspace\node\my-project\node_modules\@angular\compiler-cli\src\codegen.js:106:38)
at codegen (F:\workspace\node\my-project\node_modules\@angular\compiler-cli\src\main.js:7:81)
at Object.main (F:\workspace\node\my-project\node_modules\@angular\tsc-wrapped\src\main.js:30:16)
at Object.<anonymous> (F:\workspace\node\my-project\node_modules\@angular\compiler-cli\src\main.js:14:9)
at Module._compile (module.js:541:32)
Compilation failed

tsconfig-aot.json:

{
"compilerOptions": {
"target": "es5",
"module": "es2015",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
},
"exclude": [
"node_modules",
"typings"
],
"angularCompilerOptions": {
"genDir": "aot",
"skipMetadataEmit" : true
}
}

包.json:

{
"name": "my-project",
"version": "1.0.0",
"scripts": {
"postinstall": "typings install"
},
"license": "ISC",
"dependencies": {
"@angular/common": "~2.0.1",
"@angular/compiler": "~2.0.1",
"@angular/compiler-cli": "^0.6.0",
"@angular/core": "~2.0.1",
"@angular/forms": "~2.0.1",
"@angular/http": "~2.0.1",
"@angular/platform-browser": "~2.0.1",
"@angular/platform-browser-dynamic": "~2.0.1",
"@angular/platform-server": "^2.2.1",
"@angular/router": "~3.0.1",
"@angular/upgrade": "~2.0.1",
"body-parser": "^1.15.2",
"cookie-parser": "^1.4.3",
"core-js": "^2.4.1",
"crypto": "0.0.3",
"express": "^4.14.0",
"express-mysql-session": "^1.2.0",
"express-session": "^1.14.1",
"hammerjs": "^2.0.8",
"reflect-metadata": "^0.1.8",
"rxjs": "5.0.0-beta.12",
"systemjs": "0.19.39",
"zone.js": "^0.6.25",

// Other dependancies


},
"devDependencies": {
"concurrently": "^3.0.0",
"typescript": "^2.0.3",
"typings": "^1.4.0"
}
}

索引.哈巴狗:

html
head
base(href=config.baseUrl)
title My title
meta(charset='UTF-8')
meta(name='viewport', content='width=device-width, initial-scale=1')
link(rel='stylesheet', href="node_modules/ng2-toastr/bundles/ng2-toastr.min.css")
link(rel='stylesheet', href='node_modules/ng2-material/ng2-material.css')
link(rel='stylesheet', href='public/styles/index.css')
script(src='node_modules/core-js/client/shim.min.js')
script(src='node_modules/zone.js/dist/zone.js')
script(src='node_modules/reflect-metadata/Reflect.js')
script(src='node_modules/systemjs/dist/system.src.js')
script(src='systemjs.config.js')
script
| System.import('app')
| .then(module => module.main(!{JSON.stringify(config)}), console.error.bind(console) );
body
tt-app My application, loading waiter application ...

main.ts:

/// <reference path="../../typings/index.d.ts" />


import {platformBrowserDynamic} from "@angular/platform-browser-dynamic";
import {createMainModule} from "./app.module";

export const main = (SERVER_CONFIG) => {
platformBrowserDynamic().bootstrapModule(createMainModule(SERVER_CONFIG));
};

应用程序模块.ts:

import {NgModule} from "@angular/core";
import {BrowserModule} from "@angular/platform-browser";
import {AppComponent} from "./app/app.component";
import {routing} from "./app.routing";
import {HttpModule} from "@angular/http";
import {AuthService} from "./auth.service";
import {ConfigService} from "./config.service";
import {CustomersModule} from "./customers/customers.module";

export const createMainModule = (SERVER_CONFIG) => {

@NgModule({
imports: [
BrowserModule,
routing,
HttpModule,
],
declarations: [AppComponent],
providers: [
AuthService,
{
provide: "SERVER_CONFIG",
useValue: SERVER_CONFIG
},
ConfigService
],
bootstrap: [AppComponent]
})
class AppModule {
}
return AppModule;
};

应用程序组件.ts:

import {Component, OnInit} from "@angular/core";
import {ConfigService} from "../config.service";
import {AuthService} from "../auth.service";
import {Router} from "@angular/router";

@Component({
selector: 'tt-app',
templateUrl: 'partials/app/app.component'
})
export class AppComponent implements OnInit {

user;

constructor(private _configServer: ConfigService,
private _authService: AuthService,
private _router: Router) {
}

ngOnInit(): void {
this.user = this._configServer.server.user;
}

logout() {
this._authService
.logout()
.then((result)=> {
})
.catch((error)=> {
this._router.navigate(['/login']);
});
}

}

更新 1: 如评论中所述,我验证了如果我删除了用于将数据从服务器传递到 Angular2 的静态提供程序的代码,即 createMainModule() 在 app.module.ts 中,等等,然后 ngc 工作正常。

更新 2:我使用 SystemJS 作为模块加载器。

最佳答案

AppComponent 添加到 bootstrap 而不是 AuthComponent 并将 AppComponent 添加到 declarations还有:

@NgModule({
imports: [
BrowserModule,
SharedModule,
authRouting,
LoginModule
],
providers: [
AuthService,
{
provide: "SERVER_CONFIG",
useValue: SERVER_CONFIG
},
ConfigService
],
declarations: [AppComponent, AuthComponent],
bootstrap: [AppComponent]
})
class AuthModule {
}

关于Angular2,AoT编译: Cannot determine the module for component AppComponent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40724815/

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