您好我刚刚使用简单的
从CLI创建了一个新项目ng new frontend
命令然后,一旦我进入我的项目:
cd frontend/src/app
并尝试生成一个像
这样的新组件ng generate component about
我在终端(MacOS)中收到以下错误消息: 找不到NgModule。使用skip-import选项跳过NgModule中的导入。
我在全球范围内安装了Angular 2,当我运行
时ng --version
我收到以下内容:
Angular CLI: 6.0.0
Node: 8.11.1
OS: darwin x64
Angular:
...
Package Version
------------------------------------------------------
@angular-devkit/architect 0.6.0
@angular-devkit/core 0.6.0
@angular-devkit/schematics 0.6.0
@schematics/angular 0.6.0
@schematics/update 0.6.0
rxjs 6.1.0
typescript 2.7.2
我的Angular.json
内有:
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"frontend": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/frontend",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.css"
],
"scripts": []
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "frontend:build"
},
"configurations": {
"production": {
"browserTarget": "frontend:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "frontend:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"karmaConfig": "src/karma.conf.js",
"styles": [
"styles.css"
],
"scripts": [],
"assets": [
"src/favicon.ico",
"src/assets"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
},
"frontend-e2e": {
"root": "e2e/",
"projectType": "application",
"architect": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "frontend:serve"
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": "e2e/tsconfig.e2e.json",
"exclude": [
"**/node_modules/**"
]
}
}
}
}
},
"defaultProject": "frontend"
}
进一步挖掘:package.json
{
"name": "frontend",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "^6.0.0",
"@angular/common": "^6.0.0",
"@angular/compiler": "^6.0.0",
"@angular/core": "^6.0.0",
"@angular/forms": "^6.0.0",
"@angular/http": "^6.0.0",
"@angular/platform-browser": "^6.0.0",
"@angular/platform-browser-dynamic": "^6.0.0",
"@angular/router": "^6.0.0",
"core-js": "^2.5.4",
"rxjs": "^6.0.0",
"zone.js": "^0.8.26"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.6.0",
"@angular/cli": "~6.0.0",
"@angular/compiler-cli": "^6.0.0",
"@angular/language-service": "^6.0.0",
"@types/jasmine": "~2.8.6",
"@types/jasminewd2": "~2.0.3",
"@types/node": "~8.9.4",
"codelyzer": "~4.2.1",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~1.7.1",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~1.4.2",
"karma-jasmine": "~1.1.1",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.3.0",
"ts-node": "~5.0.1",
"tslint": "~5.9.1",
"typescript": "~2.7.2"
}
}
我尝试了什么:
我试过重新安装Angular。我已经确认我的TypeScript是最新的。我的NPM是最新的,依此类推。我已按照Angular.io
提供的教程的最基本步骤创建了一个新项目,但无效。
如果有人能告诉我这里出了什么问题,我会非常感激。
在撰写这篇文章时,我有一个很好的想法,即通过我的IDE终端运行这个过程,并且令人震惊ng generate component about
没有问题。然而,好奇并想知道我的母语终端有什么问题,我认为我毕竟是在发帖。
编辑2:
所以随意的想法突然出现在我脑海中,简单的文件权限......是的
使用sudo ng g c about
工作。所以我想你有它。希望这对那里的人有帮助。
答案 0 :(得分:4)
您应该在应用程序文件夹中运行此命令'ng g c component-name'。
1)cd src / app 2)ng g c组件名称
答案 1 :(得分:1)
就我而言,问题出在angular.json文件中。我必须删除angular.json文件中的所有“ projectname-e2e”项目才能解决此问题。
答案 2 :(得分:1)
使用Angular @ 7 ng g c
从根应用程序文件夹运行。无需更改任何内容-只需从基本目录运行类似
ng g c features/shopping-list/components/shopping-list-edit
=>
CREATE src/app/features/shopping-list/components/shopping-list-edit/shopping-list-edit.component.ts (316 bytes)
答案 3 :(得分:1)
找不到ngModule可以通过传递--module=<path to app.module.ts>
有多种解决此问题的方法,其中一种如下,
ng g c components/list-template --module=modules/app.module.ts
有关生成组件的其他选项,请参见https://angular.io/cli/generate#component
答案 4 :(得分:0)
生成component
并解析最接近的NgModule
的代码非常简单。你可以在https://github.com/angular/devkit/blob/master/packages/schematics/angular/component/index.ts
它不依赖于打字稿,npm等版本。只有在@ angular / cli版本上(并且间接在devkit上),在你的情况下是最新版本。所有人都应该找到工作,至少在我的最终确实如此。
答案 5 :(得分:0)
将root
更改为模块在angular.json
中的目录
发件人:
root:""
到应用程序模块目录:
root: "/src/app"