Component is not part of any NgModule or the module has not been imported into your module
I am building an angular 4 application. I am getting error
Error:Component HomeComponent is not part of any NgModule or the module has not been imported into your module.
I have created HomeModule and HomeComponent. Which one do I need to refer to the AppModule? I am a bit confused. Do I need to refer HomeModule or HomeComponent? Ultimately what I am looking for is when the user clicks the Home menu, he should be directed to the home.component.html which will be rendered on the index page.
App.module is:
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 is:
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 is:
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() {
}
}
Solution 1:
If your are not using lazy loading, you need to import your HomeComponent in app.module and mention it under declarations. Also, don't forget to remove from imports
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';
// import HomeComponent here
@NgModule({
declarations: [
AppComponent,
FooterbarComponent,
TopbarComponent,
NavbarComponent,
// add HomeComponent here
],
imports: [
BrowserModule,
HttpModule,
AppRoutingModule,
HomeModule // remove this
],
providers: [MRDBGlobalConstants],
bootstrap: [AppComponent]
})
export class AppModule { }
Solution 2:
In my case, I only need to restart the server (that is if you're using ng serve
).
It happens to me every time I add a new module while the server is running.
Solution 3:
In my case, I was missing my new generated component in the declarations
at app.module.ts
:
@NgModule({
declarations: [
AppComponent,
....
NewGeneratedComponent, //this was missing
....
],
imports: [
....
Solution 4:
I had the same problem. The reason was because I created a component while my server was still running. The solution is to quit the server and ng serve again.