本文介绍了使用Angular CLI生成组件时出错:无法读取null的属性"0"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个带有angular-cli的项目,并使用ng g component <componentname>命令创建了一些组件.在进行了一些管理并准备了代码以进行进一步的开发之后,突然我在运行同一命令时得到了Error: Cannot read property '0' of null. ng g service services/<servicename>可以正常工作.

I have created a project with angular-cli and created some components using the ng g component <componentname> command. After doing some administration and preparing the code for further development, and suddenly I get Error: Cannot read property '0' of null when running the same command. ng g service services/<servicename> works without any issues.

我的.angular-cli.json如下:

my .angular-cli.json is as follows:

{
  "$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复制到了失败的项目中.然后我重新执行了失败的命令,它成功了.

UPDATE: I generated a new app with ng new testApp and copied the add.module.ts into the failing project. Then I reran the failing command, and it worked.

这是失败的app.module.ts:

here is the failing 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 { }

推荐答案

很大程度上取决于您执行的管理"任务.您是否完全移动了应用程序,还是在应用程序内移动了文件或子文件夹?参见答案(如果没有,还可以找到其他建议)帮不上忙).希望对您有所帮助.

Much dependent on what 'administration' tasks you did. Did you move the app altogether or moved files or sub-folders within the app? See this answer (and also other recommendations there if that doesn't help). Hope it helps.

这篇关于使用Angular CLI生成组件时出错:无法读取null的属性"0"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 04:07