使用Angular CLI生成组件时出错:无法读取属性' 0'为null

时间:2017-09-26 18:07:43

标签: angular angular-cli

我使用angular-cli创建了一个项目,并使用ng g component <componentname>命令创建了一些组件。在做了一些管理并准备好进一步开发的代码之后,突然我在运行相同的命令时得到Error: Cannot read property '0' of nullng g service services/<servicename>无任何问题。

我的.angular-cli.json如下:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    "name": "dd-frontend"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "app",
      "styles": [
        "styles.css",
        "../node_modules/bootstrap/dist/css/bootstrap.css"
      ],
      "scripts": [
        "../node_modules/jquery/dist/jquery.js",
        "../node_modules/tether/dist/js/tether.js",
        "../node_modules/bootstrap/dist/js/bootstrap.js"
        ],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "lint": [
    {
      "project": "src/tsconfig.app.json",
      "exclude": "**/node_modules/**"
    },
    {
      "project": "src/tsconfig.spec.json",
      "exclude": "**/node_modules/**"
    },
    {
      "project": "e2e/tsconfig.e2e.json",
      "exclude": "**/node_modules/**"
    }
  ],
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "component": {}
  }
}

更新:我使用ng new testApp生成了一个新应用,并将add.module.ts复制到了失败的项目中。然后我重新命令失败的命令,它起作用了。

这是失败的app.module.ts:

//core imports
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { FormsModule } from '@angular/forms';
import { FlashMessagesModule } from 'angular2-flash-messages';

//components
import { AppComponent } from './app.component';
import { AboutComponent } from './components/about/about.component';
import { LoginComponent } from './components/login/login.component';
import { DashboardComponent } from './components/dashboard/dashboard.component';

//Services 
import { AuthService } from './services/auth.service';
//import { AuthGuard } from './guards/auth.guard';

const appRoutes: Routes = [
{ path: '', component: DashboardComponent },
{ path: 'login', component: LoginComponent },
{ path: 'about', component: AboutComponent }
];

@NgModule({
declarations: [
AppComponent,
AboutComponent,
LoginComponent,
DashboardComponent
],
imports: [
BrowserModule,
FormsModule,
RouterModule.forRoot(appRoutes),
FlashMessagesModule
],
providers: [
AuthService/*,
AuthGuard*/
],
bootstrap: [AppComponent]
})
export class AppModule { }

1 个答案:

答案 0 :(得分:0)

很大程度上取决于您执行的“管理”任务。您是否完全移动了应用程序或移动了应用程序中的文件或子文件夹?如果没有帮助,请参阅this答案(以及其他建议)。希望它有所帮助。