【问题标题】:Ionic 4 custom environment fileIonic 4 自定义环境文件
【发布时间】:2020-01-27 16:45:39
【问题描述】:

我正在使用 Ionic 4 项目。我有不同的环境(test、testB、prob、dev ecc..)。我已经在环境文件夹中创建了所有文件:environment.dev.ts、environment.testA.ts、environment.testB.ts 等。有没有办法在构建或服务时设置正确的环境(在浏览器上)项目(也许像 --env=testA 这样的角度?)?我唯一能找到的是--prod,它只适用于生产环境,但是在这种情况下,我有两个以上的环境。

【问题讨论】:

    标签: angular environment-variables ionic4


    【解决方案1】:

    您应该检查您的 package.json 文件。
    我的 package.json 如下所示:

      "name": "name",
      "version": "0.0.0",
      "license": "MIT",
      "scripts": {
        "ng": "ng",
        "start": "ng serve",
        "build": "ng build --prod",
        "test": "ng test",
       "lint": "ng lint",
       "e2e": "ng e2e",
       "build-prod": "ng build --prod --progress false",
       "build-dev": "ng build --env=dev --aot=true --output-hashing=all --sourcemaps=false --extract-css=true --named-chunks=false --build-optimizer=true --progress false",
       "build-preprod": "ng build --env=preprod --aot=true --output-hashing=all --sourcemaps=false --extract-css=true --named-chunks=false --build-optimizer=true --progress false",
       "build-dev2": "ng build --env=dev2 --aot=true --output-hashing=all --sourcemaps=false --extract-css=true --named-chunks=false --build-optimizer=true --progress false"
    },
    

    在 angular-cli.json 中有一个环境键:

    "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts",
        "preprod": "environments/environment.preprod.ts",
        "dev2": "environments/environment.dev2.ts"
      },
    

    在 Angular 8 中,您应该检查 angular.json:

    "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"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }, "preprod": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.preprod.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"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }
          }
        },
    

    【讨论】:

    • 感谢您的回答。这适用于 Angular 项目,但不适用于 Ionic,我正在使用 Ionic 和 Angular。
    • 我有一个带有 Angular 的 ionic 应用程序,根目录中有一个 angular.json,其中包含上述内容
    • 感谢您的编辑和再次回答。但是,离子 4 中没有 angular-cli.json 文件。顺便说一句,我经过一番研究后解决了。 Ionic 4 中的逻辑相似,但有一些不同,我也在此处发布我的解决方案以帮助其他人。
    • 是的,这是我提到的第二部分:在 Angular 8 中,您应该检查 angular.json。你犯了一个错误,angular.json(正如我提到的)而不是 angular.js
    • 使用 React 的 Ionic 项目怎么样?
    【解决方案2】:

    经过一番测试,我解决了! Ionic 4 的解决方案:

    1. 使用文件夹 src/environments 中的环境变量创建您需要的所有文件。示例 environment.live.ts、environment.test1.ts、environment.test2.ts 等
    2. 更新根文件夹中的angular.js文件;

        "configurations": {
             "production": { ... } // already there for production
             // Add for your custon environments files here
             "testA": {
                "fileReplacements": [
                  {
                    "replace": "src/environments/environment.ts",
                    "with": "src/environments/environment.testa.ts" //Name of your file
                  }
                ]
             } ... // Do this for every environment on every build you need
        }
      
    3. 在您需要的项目中导入环境文件:

      从'../../environments/environment'导入{环境};

    4. 在构建或服务时选择不同的环境:ionic serve -- -c=testA 构建等的相同逻辑...这将自动将环境文件替换为正确的文件,在这种情况下,文件与 testA 环境相关。

    【讨论】:

      猜你喜欢
      • 2019-04-10
      • 1970-01-01
      • 2011-12-25
      • 1970-01-01
      • 1970-01-01
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 2019-12-19
      相关资源
      最近更新 更多