如何在angular6中提供自定义环境

时间:2018-05-06 15:13:59

标签: angular

根据角度6的自述文件:

## Development server

Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files.

在我本地环境的6之前,我会运行以下内容:

  ng serve --env=local

好吧不再......

在angular.json中,我添加了以下内容:

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

那我该如何服务我的本地环境?

我尝试了以下内容:

ng serve --configuration=local
Configuration 'local' could not be found in project

2 个答案:

答案 0 :(得分:2)

假设您在 angular.json local部分下添加了build配置,您还需要在serve部分添加一个。

这样的事情:

"serve": {
  ...
  "configurations": {
    "production": {
      "browserTarget": "programName:build:production"
    },
    "local": {
      "browserTarget": "programName:build:local"
    }
  }

然后您可以像这样提供应用程序:

ng s -c=local

答案 1 :(得分:0)

应该看起来像这样。尽管我进行了设置,但似乎在Angular项目中进行了少量更改之后就可以构建整个项目。还有其他人遇到这个问题吗?

"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
            },
            "dev": {
              "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": "serverbin-angular:build"
          },
          "configurations": {
            "prod": {
              "browserTarget": "projectname-angular:build:production"
            },
            "dev": {
              "browserTarget": "serverbin-angular:build:dev"
            },
            "local": {
              "browserTarget": "serverbin-angular:build:local"
            }
          }
        }