如何在离子构建中指定 angular 环境?

How can I specify an angular environment on ionic build?

Ionic 框架使用 Angular.
Angular 6 定义了 ./src/environments/environment.stage.ts 中的环境。

在构建 Angular 应用程序时,我可以 select 在 Angular 中使用参数 --env=stage--configuration==stage 的环境 6.

为了构建 ionic 应用程序,我使用 ionic cordova build <platform> 在后台首先构建 angular 应用程序,然后将其打包到 Cordova 框架中。

如何为 angular 构建指定环境或配置?

我认为没有任何方法可以将您的 Angular 本机参数传递给 Ionic 应用程序。

但 Cordova 为您提供了大量的可能性来管理您的应用程序,例如 hooks 功能(阅读 here) or by passing a particular config file (see how the --buildConfig flag works here)。

我真的不知道你想在这里实现什么,但不同的环境有时只意味着将不同的配置文件传递给你的 cordova 构建命令。以正确的方式启动它取决于您。

编辑 :

您可能正在寻找 ionic cordova build --prod 命令...请参阅完整文档 here

希望对您有所帮助...

在我手上,我创建了一个简单的 bash 文件。

#!/usr/bin/env bash

env=

targetFile=$PWD/src/environment/environment.ts
filePath=$PWD/src/environment/.environment.ts

echo REPLACING FILE ENVIRONMENT : 
cp $filePath $targetFile

我在 .gitignore 中添加了 environment.ts,并创建了 dev.environment.tsprod.environment.ts

我切换:

$ bash launcher.sh dev && ionic serve

你可以在angular.json at ionic-cordova-build:

中添加相应的配置入口
"ionic-cordova-build": {
  "builder": "@ionic/angular-toolkit:cordova-build",
  "options": {
    "browserTarget": "app:build"
  },
  "configurations": {
    "production": {
      "browserTarget": "app:build:production"
    },
    "staging": {
      "browserTarget": "app:build:staging"
    }
  }
},
$ ionic cordova run android --device -c staging

注意离子发球上 -c staging-- -c=staging 的区别。

配置暂存必须存在于同一文件的 architect.build.configurations 下。