10

我刚刚将 angular cli 更新到版本 6。当然,我的应用程序已损坏,并且一直在尝试查看文档和 SO 问题,试图弄清楚如何将环境添加到 angular.json?我希望能够运行相当于 ng serve --env=local 的功能,然后让它在本地服务器中运行本地构建......我已经完成了一部分,我的 angular.json 文件看起来像这样......

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "bb-admin": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {
        "@schematics/angular:component": {
          "styleext": "sass"
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/bb-admin",
            "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.sass"
            ],
            "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
            },
            "local": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.local.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": "bb-admin:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "bb-admin:build:production"
            },
            "local": {
              "browserTarget": "bb-admin:build:local"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "bb-admin: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.sass"
            ],
            "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/**"
            ]
          }
        }
      }
    },
    "bb-admin-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "bb-admin:serve"
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "bb-admin"
}

但是当我运行 ng serve --configuration=local

我得到了错误。

必须指定路由或分析模块中的错误!

造成这种情况的原因是如何运行不同的环境文件来在本地构建和运行 cli 服务器?谢谢!

4

2 回答 2

5

AFAIK 这还没有真正记录在任何地方。但是,您应该能够覆盖您使用 browserTarget 指定的构建选项。或者,在构建命令部分指定另一个构建配置。

到目前为止我发现的最好的“文档”在这里https://blog.ninja-squad.com/2018/05/04/angular-cli-6.0/

更新:这个问题有一个问题,https://github.com/angular/angular-cli/issues/10612

还有一些不是特别令人惊奇的文档https://github.com/angular/angular-cli/wiki/angular-workspace。现在,您必须单独指定每个配置,而不考虑重叠选项。我能想到的唯一“解决方法”是使用其他一些实用程序来合并 json 文件 preserve/build

    "serve": {
      "builder": "@angular-devkit/build-angular:dev-server",
      "options": {
        "browserTarget": "bb-admin:build"
      },
      "configurations": {
        "local": {
          "browserTarget": "bb-admin:build:local"
        }
      }
    },
于 2018-05-23T13:41:14.240 回答
1

我在“脚本”下添加了 package.json 之类的东西

"myscript": "ng serve -c local-version"

比我在“配置”下添加到 angular.json

"local-version": {
            "fileReplacements": [
                {
                    "replace": "src/environments/environment.ts",
                    "with": "src/environments/environment.local.ts"
                }
            ]
        }

在“服务”下

"configurations": {
        "local-version": {
          "browserTarget": "your-app:build:local-version"
        }

我启动应用程序:

npm run myscript

当然你必须创建一个名为 environment.local.ts 的文件

于 2018-11-30T13:19:05.900 回答