问题
My current application was in Angular 4 without Angular CLI. So I never had Angualr.json file or Angular-cli.json file. Now we want to generate that Angular CLI or angular.json file. Because this file is not available it is not taking any of the ng commands like ng serve or ng g c testing
How can I generate the file without using ng new command because ng new will create a new project which I don't want. I want to support my existing project.
Few notes on my project : 1. We are launching multiple application from one folder 2. We are launching each application from the ASPX page without using index.html.
Please see the screen shot attached of the error and the folder structure of my projectenter image description here
enter image description here
回答1:
Create a new file with name '.angular-cli.json' and add this file in your main directory.
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"project": {
"name": "my-app"
},
"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"
],
"scripts": [],
"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": {}
}
}
回答2:
From angular >= 6 '.angular-cli.json' file is replaced by 'angular.json' file. as your project is based on angular 4 that you have said so you need to create '.angular-cli.json' file not 'angular.json' file.
As you have ready made project of angular 4 and you want to generate file command (as you mentioned above) like :
ng g c sample
so i assume you have installed latest angular cli in globally.
you can make a trick. Just create a new project in other location and copy that '.angular-cli.json' file in your desired project folder. after that just change the project name and other things which is relevant to your need. create a '.angular-cli.json' file in own hand is little bit tedious and time consuming also as you know.And if you already have cli installed then i think no need to create it manually which may cause you compiling error also.That's all my friend. you can also use this '.angular-cli.json' below if you want:
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"project": {
"name": "angular-app"
},
"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"
],
"scripts": [],
"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"
},
{
"project": "src/tsconfig.spec.json"
},
{
"project": "e2e/tsconfig.e2e.json"
}
],
"test": {
"karma": {
"config": "./karma.conf.js"
}
},
"defaults": {
"styleExt": "css",
"component": {}
}
}
来源:https://stackoverflow.com/questions/55038367/how-to-generate-angular-json-file