组件不是任何 NgModule 的一部分,或者该模块尚未导入到您的模块中

新手上路,请多包涵

我正在构建一个 Angular 4 应用程序。我收到错误

Error:Component HomeComponent is not part of any NgModule or the module has not been imported into your module.

我创建了 HomeModule 和 HomeComponent。我需要参考哪一个 AppModule?我有点困惑。我需要参考 HomeModule 还是 HomeComponent?最终我要寻找的是当用户单击主页菜单时,他应该被定向到 home.component.html 将呈现在索引页面上。

App.module 是:

 import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http'

import { AppComponent } from './app.component';
import { NavbarComponent } from './navbar/navbar.component';
import { TopbarComponent } from './topbar/topbar.component';
import { FooterbarComponent } from './footerbar/footerbar.component';
import { MRDBGlobalConstants } from './shared/mrdb.global.constants';
import { AppRoutingModule } from './app.routing';
import { HomeModule } from './Home/home.module';

@NgModule({
  declarations: [
    AppComponent,
    FooterbarComponent,
    TopbarComponent,
    NavbarComponent

  ],
  imports: [
    BrowserModule,
    HttpModule,
    AppRoutingModule,
    HomeModule

  ],
  providers: [MRDBGlobalConstants],
  bootstrap: [AppComponent]
})
export class AppModule { }

HomeModule 是:

 import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HomeComponent } from './home.component';

@NgModule({
  imports: [
    CommonModule
  ],
  exports: [HomeComponent],
  declarations: [HomeComponent]
})
export class HomeModule { }

HomeComponent 是:

 import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

原文由 Tom 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 583
1 个回答

我收到此错误是因为我在 2 个不同的模块中具有相同的组件名称。一种解决方案是,如果它共享使用导出技术等,但在我的情况下,两者都必须命名相同,但目的不同。

真正的问题

因此,在导入组件 B 时,智能感知导入了组件 A 的路径,因此我必须从智能感知中选择组件路径的第二个选项,这解决了我的问题。

原文由 Wahab Shah 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进