Reputation: 735
I have been trying to generate new components using Angular2's CLI tool using this command:
ng generate component test
After execution, new component files are generated, but the references are not added into the app.module.ts file:
No app module found. Please add your new class to your component.
I was trying to find the solution to fix this so I dont have to always import new component and add it to declarations manually, but I couldn't find anything about this issue online.
I think it may have something to do with my angular-cli.json file, but it seems to be ok:
Any ideas?
UPDATE: This is my project folder structure, simplyfied. I removed folders which are not relevant:
Code in the app.module.ts is following:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { SearchComponent } from './search/search.component';
@
NgModule({
declarations: [
AppComponent,
SearchComponent
],
imports: [
BrowserModule,
HttpModule
],
providers: [
],
bootstrap: [
AppComponent
]
})
export class AppModule { }
Upvotes: 4
Views: 6678
Reputation: 735
The issue why angular-cli couldn't find app module was that the "@" sign and the "NgModule" name where not on the same line. This does not make the code fail on execution, but it prevents the CLI from detecting the module:
@
NgModule({
if you write both on the same line, cli is able to find the module successfully:
@NgModule({
Upvotes: 1
Reputation: 1578
It No Problem. But Not added, your installed component. So add your component name to app.module.ts.
example
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 { HeaderComponent } from './header/header.component';<!-- My Installed Component -->
import { FooterComponent } from './footer/footer.component';<!-- My Installed Component -->
@NgModule({
declarations: [
AppComponent,
HeaderComponent,<!-- My Installed Component -->
FooterComponent<!-- My Installed Component -->
],
imports: [
BrowserModule,
FormsModule,
HttpModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Upvotes: 0