【问题标题】:Error: Error: No NgModule metadata found for 'AppComponent'错误:错误:找不到“AppComponent”的 NgModule 元数据
【发布时间】:2017-05-16 16:33:10
【问题描述】:

我正在尝试将 Django 与 Angular2 一起使用。我有一个工作的 Django 项目。我在尝试使用 Angular2 时遇到此错误:

错误:错误:找不到“AppComponent”的 NgModule 元数据。
在新的 BaseException (http://user:8000/ng/node_modules/@angular/compiler//bundles/compiler.umd.js:5116:27)
在 NgModuleResolver.resolve (http://user:8000/ng/node_modules/@angular/compiler//bundles/compiler.umd.js:12956:27)
在 CompileMetadataResolver.getNgModuleMetadata (http://user:8000/ng/node_modules/@angular/compiler//bundles/compiler.umd.js:13200:51)
在 RuntimeCompiler._compileComponents (http://user:8000/ng/node_modules/@angular/compiler//bundles/compiler.umd.js:15845:51)
在 RuntimeCompiler._compileModuleAndComponents (http://user:8000/ng/node_modules/@angular/compiler//bundles/compiler.umd.js:15769:41)
在 RuntimeCompiler.compileModuleAsync (http://user:8000/ng/node_modules/@angular/compiler//bundles/compiler.umd.js:15746:25)
在 PlatformRef__._bootstrapModuleWithZone (http://user:8000/ng/node_modules/@angular/core//bundles/core.umd.js:9991:29)
在 PlatformRef_.bootstrapModule (http://user:8000/ng/node_modules/@angular/core//bundles/core.umd.js:9984:25)
在 Object.eval (http://user:8000/ng/src/main.js:5:53)
在评估 (http://user:8000/ng/src/main.js:17:4)
评估http://user:8000/ng/src/main.js
加载时出错http://user:8000/ng/src/main.js

package.json 文件的一部分:

"dependencies": {
"@angular/common":  "2.0.0-rc.5",
"@angular/compiler":  "2.0.0-rc.5",
"@angular/core":  "2.0.0-rc.5",
"@angular/forms": "0.2.0",
"@angular/http":  "2.0.0-rc.5",
"@angular/platform-browser":  "2.0.0-rc.5",
"@angular/platform-browser-dynamic":  "2.0.0-rc.5",
"@angular/router":  "3.0.0-beta.2",
"@angular/router-deprecated":  "2.0.0-rc.2",
"@angular/upgrade":  "2.0.0-rc.5",

"systemjs": "0.19.27",
"core-js": "^2.4.0",
"reflect-metadata": "^0.1.3",
"rxjs": "5.0.0-beta.6",
"zone.js": "^0.6.12",

"angular2-in-memory-web-api": "0.0.15",
"bootstrap": "^3.3.6"},

main.ts:

import { bootstrap } from '@angular/platform-browser-dynamic';
import { AppComponent } from './app/app.component';
import {appRouterProviders} from "./app/app.route";
import {HTTP_PROVIDERS} from "@angular/http";
import {enableProdMode} from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module'; <-- Added this as per an answer below. Error is still there. 
platformBrowserDynamic().bootstrapModule(AppModule);

app.module.ts:

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule } from '@angular/router';
import { routes } from './app.routes';

import { AppComponent }  from './app.component';

@NgModule({
  imports:      [ BrowserModule,FormsModule,HttpModule, 
RouterModule.forRoot( ROUTES, { useHash: true } ) ],
declarations: [ AppComponent ],
bootstrap:    [ AppComponent ]
})
export class AppModule { }

index.html:

<script>
  window.ANGULAR_URL = "{{ ANGULAR_URL }}";
  System.import('/ng/src/main.js').catch(function(err){
      console.error(err);
  });
</script>

我在 Django 的 settings.py 文件中设置了 ANGULAR_URL。它在浏览器上显示 /ng/。我进行了很多搜索并尝试多次更改package.json 中的依赖项。每次我在package.json 中进行更改时,我都会确保我做了npm install。我已经看到许多不同版本的语法变化。如果这可能是问题,这里是我正在使用的 npm 和节点版本: npm 4.1.1node v4.8.3。我在 8000 端口上运行 Django。

app.component.ts 文件:

import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';
@Component({
selector: 'my-app',
template: `
    <h1>My First Angular 2 App</h1>
    <nav>
      <a routerLink="/component1" routerLinkActive="active">Component 1</a>
      <a routerLink="/component2" routerLinkActive="active">Component 2</a>
    </nav>
    <router-outlet></router-outlet>
`,
directives: [ROUTER_DIRECTIVES]
})
export class AppComponent { }

UPDATE-1

package.json 文件的脚本部分:

"scripts": {
"start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
"docker-build": "docker build -t ng2-quickstart .",
"docker": "npm run docker-build && docker run -it --rm -p 3000:3000 -p 3001:3001 ng2-quickstart",
"pree2e": "npm run webdriver:update",
"e2e": "tsc && concurrently \"http-server -s\" \"protractor protractor.config.js\" --kill-others --success first",
"lint": "tslint ./app/**/*.ts -t verbose",
"lite": "lite-server",
"postinstall": "typings install",
"test": "tsc && concurrently \"tsc -w\" \"karma start karma.conf.js\"",
"test-once": "tsc && karma start karma.conf.js --single-run",
"tsc": "tsc",
"tsc:w": "tsc -w",
"typings": "typings",
"webdriver:update": "webdriver-manager update"
},

顺便说一句,我一直在关注this 使用 Angular 和 Django。

UPDATE-2

我刚刚意识到我的 tsconfig.json 和 typings.json 不在同一个地方。我把它们放在同一个地方,因为 package.json 存在。现在我收到以下错误:

错误 TS5023:未知的编译器选项“lib”。

UPDATE-3

tsconfig.json 文件:

{
 "compilerOptions": { 
 "target": "es5",
 "module": "commonjs",
 "moduleResolution": "node",
 "sourceMap": true,
 "emitDecoratorMetadata": true,
 "experimentalDecorators": true,
 "lib": [ "es2015", "dom" ],
 "noImplicitAny": true,
 "suppressImplicitAnyIndexErrors": true
 }
}

我的tsc -v 给了Version 2.1.5。现在我已将"typescript": "^1.8.10", 更改为"typescript": "^2.1.5"。所以更新 -2 中提到的错误我不存在,但它显示了这一点:

错误:TypeError:无法读取未定义的属性“forRoot”

错误:TypeError:无法读取未定义的属性“forRoot”
在 Object.eval (http://user:8000/ng/src/app/app.module.js:22:114)
在评估 (http://user:8000/ng/src/app/app.module.js:29:4)
在评估 (http://user:8000/ng/src/app/app.module.js:30:3)
在评估(匿名)
评估http://user:8000/ng/src/app/app.module.js
评估http://user:8000/ng/src/main.js
加载时出错http://user:8000/ng/src/main.js

【问题讨论】:

  • 你能发布你的 app.component.ts 吗?
  • 我已经包含了 app.component.ts 文件。

标签: javascript angularjs node.js django typescript


【解决方案1】:

您没有在 main.ts 中导入 AppModule。您可能正在引导 @​​987654322@,这导致了您看到的错误。

import { bootstrap } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import {appRouterProviders} from "./app/app.route";
import {HTTP_PROVIDERS} from "@angular/http";
import {enableProdMode} from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

platformBrowserDynamic().bootstrapModule(AppModule);

【讨论】:

  • 在 main.ts 中导入 AppModule 没有任何区别。复制我的代码时出现错误。我现在在我的问题中添加了它。
  • 修改后编译了吗?该错误表明您正在尝试引导 AppComponent
  • 是的。我确实编译了。我还在 package.json 中看到了有关脚本的建议,但没有一个有效。所以让我来看看是否存在错误。
  • 那么,我怀疑它现在没有被编译。这可能是什么原因?
  • 你是怎么编译的?您是否看到任何编译错误?发布您的 tsconfig、json 和您的 tsc --version
猜你喜欢
  • 2017-03-08
  • 2019-06-27
  • 2020-10-16
  • 2019-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
相关资源
最近更新 更多