javascript - 错误: Error: No NgModule metadata found for 'AppComponent'

标签 javascript angularjs node.js django typescript

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

Error: Error: No NgModule metadata found for 'AppComponent'.
at new BaseException (http://user:8000/ng/node_modules/@angular/compiler//bundles/compiler.umd.js:5116:27)
at NgModuleResolver.resolve (http://user:8000/ng/node_modules/@angular/compiler//bundles/compiler.umd.js:12956:27)
at CompileMetadataResolver.getNgModuleMetadata (http://user:8000/ng/node_modules/@angular/compiler//bundles/compiler.umd.js:13200:51)
at RuntimeCompiler._compileComponents (http://user:8000/ng/node_modules/@angular/compiler//bundles/compiler.umd.js:15845:51)
at RuntimeCompiler._compileModuleAndComponents (http://user:8000/ng/node_modules/@angular/compiler//bundles/compiler.umd.js:15769:41)
at RuntimeCompiler.compileModuleAsync (http://user:8000/ng/node_modules/@angular/compiler//bundles/compiler.umd.js:15746:25)
at PlatformRef_._bootstrapModuleWithZone (http://user:8000/ng/node_modules/@angular/core//bundles/core.umd.js:9991:29)
at PlatformRef_.bootstrapModule (http://user:8000/ng/node_modules/@angular/core//bundles/core.umd.js:9984:25)
at Object.eval (http://user:8000/ng/src/main.js:5:53)
at eval (http://user:8000/ng/src/main.js:17:4)
Evaluating http://user:8000/ng/src/main.js
Error loading 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 和 node 版本: 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 { }

更新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 结合使用。

更新2

我刚刚意识到我的 tsconfig.json 和 Typings.json 不在同一个地方。我将两者放在 package.json 存在的同一位置。现在我收到以下错误:

error TS5023: Unknown compiler option 'lib'.

更新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给出了版本2.1.5。现在我已将 "typescript": "^1.8.10", 更改为 "typescript": "^2.1.5"。所以 update -2 中提到的错误我不存在,但它显示了:

Error: TypeError: Cannot read property 'forRoot' of undefined

Error: TypeError: Cannot read property 'forRoot' of undefined
at Object.eval (http://user:8000/ng/src/app/app.module.js:22:114)
at eval (http://user:8000/ng/src/app/app.module.js:29:4)
at eval (http://user:8000/ng/src/app/app.module.js:30:3)
at eval (anonymous)
Evaluating http://user:8000/ng/src/app/app.module.js
Evaluating http://user:8000/ng/src/main.js
Error loading http://user:8000/ng/src/main.js

最佳答案

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

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);

关于javascript - 错误: Error: No NgModule metadata found for 'AppComponent' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44006959/

相关文章:

javascript - 如何使用 Node.js 按需解析记录?

javascript - 需要 cssSandpaper.js 帮助吗?

javascript - jQuery 高亮可点击区域

javascript - Angular : Why does field $invalid is true in other view?

javascript - 如何在 cucumber.js 的 AfterStep 钩子(Hook)中获取步骤结果

javascript - Node.js:PDFkit 写入响应

javascript - 为什么等待延迟在函数作用域结束时结束

javascript - 使用流类型限制向对象添加属性

javascript - Angular 获取选中的复选框

javascript - 如何使用js向输入字段插入文本?