如何在 Angular 中使用单个 .env 文件动态处理多个环境(例如,environment.ts、environment.uat.ts、environment.preProd.ts)?

问题描述 投票:0回答:1

我想使用单个 .env 文件来动态处理所有环境。有可能做到吗? 我尝试将不同环境的所有变量写入单个 .env 中,但这似乎不是正确的过程。 基本上,我需要使用单个 .env 文件运行我的角度应用程序,而不是编写不同的环境。

angular dynamic environment-variables vite .env
1个回答
0
投票

转到你的

angular.json
检查
fileReplacements
下名为
configurations
的属性,Angular总是只查看
environment.json
,我们用它来根据环境复制/粘贴内容,通过这个配置,如果你删除
fileReplacements
,只有一个文件,将用于所有环境。

适用于环境的 Angular 文档

"configurations": {
  "development": { … },
  "production": { … },
  "staging": {
    "fileReplacements": [
      {  // remove this!
        "replace": "src/environments/environment.ts", // remove this!
        "with": "src/environments/environment.staging.ts" // remove this!
      } // remove this!
    ]
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.