使用 Angular CLI 生成组件时出错:无法读取 null 的属性“0" [英] Error generating components with Angular CLI: Cannot read property '0' of null
问题描述
我使用 angular-cli 创建了一个项目,并使用 ng g component
命令创建了一些组件.在做了一些管理并为进一步开发准备代码后,突然我在运行相同的命令时收到 Error: Cannot read property '0' of null
.ng g service services/
工作没有任何问题.
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 { }
推荐答案
很大程度上取决于您所做的管理"任务.您是否完全移动了应用程序或移动了应用程序内的文件或子文件夹?请参阅 this 答案(如果没有,还有其他建议)没有帮助).希望有帮助.
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"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!