Angular 2 CLI "ng generate component" 命令未找到 app.module
Angular 2 CLI "ng generate component" command does not find app.module
我一直在尝试使用以下命令使用 Angular2 的 CLI 工具生成新组件:
ng generate component test
执行后,生成了新的组件文件,但没有将引用添加到app.module.ts文件中:
No app module found. Please add your new class to your component.
我试图找到解决这个问题的方法,这样我就不必总是导入新组件并将其手动添加到声明中,但是我在网上找不到关于这个问题的任何信息。
我觉得可能和我的angular-cli.json文件有关,不过好像没问题:
有什么想法吗?
更新:
这是我的项目文件夹结构,很简单。我删除了不相关的文件夹:
app.module.ts 中的代码如下:
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 { }
angular-cli 找不到应用程序模块的问题是“@”符号和 "NgModule" 名称不在同一行。这不会使代码执行失败,但会阻止 CLI 检测模块:
@
NgModule({
如果将两者写在同一行,cli 可以成功找到模块:
@NgModule({
没问题。但未添加,您安装的组件。所以将您的组件名称添加到 app.module.ts.
例子
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 { }
我一直在尝试使用以下命令使用 Angular2 的 CLI 工具生成新组件:
ng generate component test
执行后,生成了新的组件文件,但没有将引用添加到app.module.ts文件中:
No app module found. Please add your new class to your component.
我试图找到解决这个问题的方法,这样我就不必总是导入新组件并将其手动添加到声明中,但是我在网上找不到关于这个问题的任何信息。
我觉得可能和我的angular-cli.json文件有关,不过好像没问题:
有什么想法吗?
更新: 这是我的项目文件夹结构,很简单。我删除了不相关的文件夹:
app.module.ts 中的代码如下:
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 { }
angular-cli 找不到应用程序模块的问题是“@”符号和 "NgModule" 名称不在同一行。这不会使代码执行失败,但会阻止 CLI 检测模块:
@
NgModule({
如果将两者写在同一行,cli 可以成功找到模块:
@NgModule({
没问题。但未添加,您安装的组件。所以将您的组件名称添加到 app.module.ts.
例子
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 { }