Angular 6.4.1 环境构建不起作用

Posted

技术标签:

【中文标题】Angular 6.4.1 环境构建不起作用【英文标题】:Angular 6.4.1 environment builds not working 【发布时间】:2020-10-17 10:58:43 【问题描述】:

因此尝试执行“npm run ng build --configuration=production”并尝试让它使用我的 angular.json 中的 FileReplacements 将 environment.ts 文件替换为 environment.prod.ts

我正在使用以下版本:

"@angular/animations": "^8.0.0-beta.12",
"@angular/cdk": "~7.3.7",
"@angular/common": "^8.0.0-beta.12",
"@angular/compiler": "^8.0.0-beta.12",
"@angular/core": "^8.0.0-beta.12",
"@angular/forms": "^8.0.0-beta.13",
"@angular/material": "^7.3.7",

当我运行上面的命令来设置构建配置时,在 dist 文件夹中,我仍然看到 environment.ts 中引用的 URL,而不是 main-es5.js 文件中的 environment.prod.ts。

src/environments/environment.ts

export const environment = 
  production: false,
  baseUrl: 'http://0.0.0.0'
  
;

src/environments/environment.prod.ts

export const environment = 
  production: true,
  baseUrl: 'http://1.1.1.1'
;

如果有人发现我做错了什么,请告诉我。每次构建时都必须更改 URL 和其他变量。谢谢!

angular.json


  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": 
    "NewProject": 
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": ,
      "architect": 
        "build": 
          "builder": "@angular-devkit/build-angular:browser",
          "options": 
            "outputPath": "dist/NewProject",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/custom-theme.scss",
              "src/styles.css",
              "node_modules/ag-grid-community/dist/styles/ag-grid.css",
              "node_modules/ag-grid-community/dist/styles/ag-theme-balham.css"
            ],
            "scripts": [],
            "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": "NewProject:build"
          ,
          "configurations": 
            "production": 
              "browserTarget": "NewProject:build:production"
            
          
        ,
        "extract-i18n": 
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": 
            "browserTarget": "NewProject: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",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          
        ,
        "lint": 
          "builder": "@angular-devkit/build-angular:tslint",
          "options": 
            "tsConfig": [
              "tsconfig.app.json",
              "tsconfig.spec.json",
              "e2e/tsconfig.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          
        ,
        "e2e": 
          "builder": "@angular-devkit/build-angular:protractor",
          "options": 
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "NewProject:serve"
          ,
          "configurations": 
            "production": 
              "devServerTarget": "NewProject:serve:production"
            
          
        
      
    
  ,
  "defaultProject": "NewProject"

【问题讨论】:

如果你只做ng build --configuration=production呢? 【参考方案1】:

运行 npm run ng build --configuration=production 不会将额外的参数传递给 ng build 任务。它只会调用ng build

如果你想构建生产,你可以使用ng build --configuration=production

注意:如果你真的想使用npm run ... 来构建生产环境,你需要改变你感知参数的方式,正如here 解释的那样,并使用-- 来分隔参数以发送到任务

npm run ng build -- --configuration=production

【讨论】:

以上是关于Angular 6.4.1 环境构建不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Tailwind 自定义颜色在 Angular 生产构建中不起作用

为啥外部链接在构建后在 phonegap 应用程序上不起作用

angular 2 表单元素作为组件 - 验证不起作用

为啥媒体查询在 Angular8 中不起作用

多个 Angular 库在生产模式下不起作用

条件路由在 Angular 应用程序中不起作用