我在Angular CLI中使用Angular 8。我的应用程序设置存储在environment.ts和environment.prod.ts中。 serve 和 build 命令正在使用environment.prod.ts。
我需要 serve 命令来使用environment.ts而不是environment.prod.ts。
更新:
这是我的服务命令:
ng serve
这是我的 Angular.json 文件:
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"UI": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist",
"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",
"src/assets/vendor/fontawesome/css/all.min.css",
"src/assets/css/fonts.iransans.css",
"src/assets/css/fonts.titr.css",
"src/assets/css/fonts.yekan.css",
"node_modules/ngx-toastr/toastr.css",
"node_modules/ngx-lightbox/lightbox.css",
"src/assets/css/styles.css",
"src/assets/css/custom.css"
],
"scripts": [
"node_modules/jquery/dist/jquery.min.js",
"node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"
],
"es5BrowserSupport": true
},
"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,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
}
]
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "UI:build"
},
"configurations": {
"production": {
"browserTarget": "UI:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "UI: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": [
"src/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/**"
]
}
}
}
},
"UI-e2e": {
"root": "e2e/",
"projectType": "application",
"prefix": "",
"architect": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "UI:serve"
},
"configurations": {
"production": {
"devServerTarget": "UI:serve:production"
}
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": "e2e/tsconfig.e2e.json",
"exclude": [
"**/node_modules/**"
]
}
}
}
}
},
"defaultProject": "UI"
}
答案 0 :(得分:0)
您必须相应地编辑angular.cli.js。
"environments": {
"dev": "environments/environment.ts",
"prod": "environments/environment.prod.ts",
"qa": "environments/environment.qa.ts"
}
在发球时,您可以使用此标志传递环境。
--environment={envName}
通过阅读此article,您可以了解一个好主意。
答案 1 :(得分:0)
您正在使用哪个
ng构建/服务
或
npm构建/提供服务
如果您使用的是“ npm”, 请让您的脚本在package.json中正确
答案 2 :(得分:0)
如果您正在使用npm start
之类的服务,则由package.json中的“脚本”驱动。如果这是您启动项目的方式,请检查那里的“开始”脚本正在运行,因为它可能是用--prod
标志(例如"start": "ng serve --prod",
)设置的,或者引用了其他配置;除非它使用--prod
标志或其他配置(例如--configuration=production
)运行,否则它应该默认为environment.ts。
否则,对于angular 8,您的设置位于angular.json文件中,您应该在“项目”>“”>“建筑师”>“构建”>“配置”>“本地”下检查是否有任何fileReplacement设置,例如:
"fileReplacements": [{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}],
如果这样做,请删除“本地”的内容,使其只是空括号{}
,或将“ with”值更改为"with": "src/environments/environment.ts"
,然后再返回到从环境中拉取。或者,您可以完全进行全新的配置,并在构建中使用--configuration=<configuration name>
标志对其进行引用(但是,如果这样做,请不要忘记在“服务”>“配置”>“”下添加“ browserTarget” (例如"<project name>:build:local"
)。
答案 3 :(得分:0)
问题是VS Code的“自动导入”功能。它自动添加了错误的路径。这是导入的行:
import { environment } from './../../environments/environment.prod';
它应该是这样的:
import { environment } from './../../environments/environment';
如果有人遇到此问题,请首先检查导入路径。