简体   繁体   English

错误:找不到'AppModule'的NgModule元数据

[英]Error: No NgModule metadata found for 'AppModule'

I have a problem with an Angular 2 app that I am building. 我正在构建一个Angular 2应用程序的问题。 I have been exercising my copy/paste skills from various locations, and have eliminated all of the build errors, but when I launch it in the browser I have an error in the browser. 我一直在从各个位置练习我的复制/粘贴技能,并且已经消除了所有构建错误,但是当我在浏览器中启动它时,我在浏览器中出错。 I have looked at this post but it hasn't solved my problem. 我看过这篇文章,但它没有解决我的问题。

My AppModule looks like this: 我的AppModule看起来像这样:

import { NgModule, ApplicationRef } 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 { HomeComponent } from '../index';

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

I'm bootstrapping like this: 我是这样开始的:

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './index';

platformBrowserDynamic().bootstrapModule(AppModule);

But in my browser, I get this error: 但在我的浏览器中,我收到此错误:

ng_module_resolver.js:34 Uncaught Error: No NgModule metadata found for 'AppModule'. ng_module_resolver.js:34未捕获错误:未找到“AppModule”的NgModule元数据。

How do I solve this? 我该如何解决这个问题?

Update 更新

My code looks like this now, but still generate an error: 我的代码现在看起来像这样,但仍会生成错误:

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 { HomeComponent } from '../index';
import { AppComponent } from './app.component';

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

And bootstraping: 并且引导:

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppComponent } from './index';

platformBrowserDynamic().bootstrapModule(AppComponent);

This is the error I'm getting now: 这是我现在得到的错误:

Uncaught Error: No NgModule metadata found for 'AppComponent' 未捕获错误:未找到“AppComponent”的NgModule元数据

Is this an error with AppComponent , or the bootstrapping, or the module declaration and metadata? 这是AppComponent ,引导,模块声明和元数据的错误吗? The documentation is vague. 文件含糊不清。

There are three mistakes at least in your code. 至少在你的代码中有三个错误。

First - you are bootstraping component, this is wrong - you should bootstrap module, in your case AppModule and in AppModule you should choose the component that is going to be bootstrapped. 首先 - 你是bootstraping组件,这是错误的 - 你应该引导模块,在你的情况下AppModuleAppModule你应该选择将要引导的组件。

Second - you are specifying AppModule as a component that's going to be bootstrapped: 第二 - 您将AppModule指定为将要引导的组件:

bootstrap: [ AppModule ]

You can't bootstrap module, you can bootstrap component, for example HomeComponent . 你不能引导模块,你可以引导组件,例如HomeComponent From what I can see, that's the only component you have. 从我所看到的,这是你拥有的唯一组件。

Third - you are adding AppModule to its declarations: 第三 - 您正在将AppModule添加到其声明中:

declarations: [
  AppModule,
  HomeComponent
]

You simply cannot do this, you should only add components, directives and pipes to module's declarations and even if you could declare modules, why would you declare AppModule inside itself? 您根本无法做到这一点,您应该只向模块的声明添加组件,指令和管道,即使您可以声明模块,为什么要在其自身内部声明AppModule Try this code: 试试这段代码:

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

export class AppModule {
}

And then bootstrap AppModule : 然后引导AppModule

platformBrowserDynamic().bootstrapModule(AppModule);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找不到“ AppModule”的NgModule元数据中的错误 - ERROR in No NgModule metadata found for 'AppModule' 错误:找不到“ AppModule”的NgModule元数据 - Error: No NgModule metadata found for 'AppModule' 未找到“AppModule”的 NgModule 元数据 - No NgModule metadata found for 'AppModule' 错误:找不到“ AppModule”的NgModule元数据。 在另一台机器上 - Error: No NgModule metadata found for 'AppModule'. on another machine 找不到“AppModule”Angular 6.1.0 的 NgModule 元数据中的错误 - ERROR in No NgModule metadata found for 'AppModule' Angular 6.1.0 在“ AppModule”的NgModule元数据中找不到Angular 7错误 - Angular 7 ERROR in No NgModule metadata found for 'AppModule' 编译器失败 - 找不到“AppModule”的 NgModule 元数据中的错误 - Compiler fails - ERROR in No NgModule metadata found for 'AppModule' 带有 Angular 的 Docker 给出错误“找不到用于‘AppModule’的 NgModule 元数据中的错误”。 - Docker with Angular giving error "ERROR in No NgModule metadata found for 'AppModule'." 找不到“AppModule”ngx-rocket metronic 主题的 NgModule 元数据中的错误 - ERROR in No NgModule metadata found for 'AppModule' ngx-rocket metronic theme 找不到“ AppModule”的NgModule元数据中的错误。 和webpack:编译失败 - ERROR in No NgModule metadata found for 'AppModule'. and webpack: Failed to compile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM