"configurations": {
"production": {
// already there!
},
"development": { // this is new!
"buildOptimizer": false,
"optimization": false,
"vendorChunk": true,
"extractLicenses": false,
"sourceMap": true,
"namedChunks": true
}
},
"defaultConfiguration": "production"
要在开发模式下运行构建,只需输入以下命令:ng build --configuration development
。
architect
> serve
> configurations
,以添加指向以下开发配置的开发配置:<project>:build:development,并将"defaultConfiguration": "development"
添加到serve下,这样本地的“ng serve”命令就默认提供此服务。 - Routhinator我已经在 angular.json
中设置了所有这些配置。
对于我来说,问题是以下内容:
main.ts:
if (environment.production) {
enableProdMode(); // <-- here
}
platformBrowserDynamic()
.bootstrapModule(AppModule)
.catch(err => console.error(err));
显然,过去的某个时点,我改变了environment.ts
文件中的production
属性,将其设置为true
(即使在开发模式下)。
将其改回false
最终解决了我的问题。
顺便说一下:完整的可工作配置(见下文),如果配置不是问题,则:
ng serve
(如果是单仓库 npx nx serve <projectname>
)启动然后您会看到类似于这样的东西(取决于您自己的配置):
** Angular Live Development Server is
listening on localhost:4200,
open your browser on http://localhost:4200/ **
然后您应该可以看到:
最后但同样重要的是:完整配置示例:
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"ng14.0.2": {
"projectType": "application",
"schematics": {
"@schematics/angular:component": {
"style": "scss"
}
},
"root": "",
"sourceRoot": "src",
"prefix": "app",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/ng14.0.2",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.app.json",
"inlineStyleLanguage": "scss",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.scss"
],
"scripts": []
},
"configurations": {
"production": {
"budgets": [
{
"type": "initial",
"maximumWarning": "500kb",
"maximumError": "1mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "2kb",
"maximumError": "4kb"
}
],
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"outputHashing": "all"
},
"development": {
"buildOptimizer": false,
"optimization": false,
"vendorChunk": true,
"extractLicenses": false,
"sourceMap": true,
"namedChunks": true
}
},
"defaultConfiguration": "production"
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"configurations": {
"production": {
"browserTarget": "ng14.0.2:build:production"
},
"development": {
"browserTarget": "ng14.0.2:build:development"
}
},
"defaultConfiguration": "development"
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "ng14.0.2:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.spec.json",
"karmaConfig": "karma.conf.js",
"inlineStyleLanguage": "scss",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.scss"
],
"scripts": []
}
}
}
}
}
}
Angular DevTools 可以检测到生产配置,即使所有其他配置都已正确定义。
您的配置构建部分的此属性将激活 Angular DevTools;)
"optimization": true,
到
"optimization": false,
ng build -c development
。 - N.F.