简体   繁体   English

Angular 中的操作系统环境变量

[英]OS environment variables in Angular

I know about the environment.ts files in Angular, but with those I end up comitting sensitive data to my git repo.我知道 Angular 中的 environment.ts 文件,但是我最终会将敏感数据提交到我的 git 存储库中。 In Java I can just refer to OS environment variables which I can set on my server.在 Java 中,我可以只引用可以在服务器上设置的操作系统环境变量。

In our company the CI dockers all our applications and pushes them into OpenShift, which means I don't have access to the file system, so I can not just put a production environment.ts there manually.在我们公司,CI 将我们所有的应用程序 dockers 并将它们推送到 OpenShift,这意味着我无法访问文件系统,因此我不能只是手动将生产环境.ts 放在那里。

Does anyone have an idea how to get to the OS environment variables?有谁知道如何获得操作系统环境变量?

If you are bundling your angular application with webpack, you can use webpack.DefinePlugin .如果您将 angular 应用程序与 webpack 捆绑在一起,则可以使用webpack.DefinePlugin

plugins: [
    new webpack.DefinePlugin({
        "some_variable": JSON.stringify(process.env.SOME_ENV_VAR || "my_default_value")
    })
]

Then you can reference "some_variable" in your angular application.然后你可以在你的角度应用程序中引用“some_variable”。 You can declare them in your typings.d.ts file so they are recognized.您可以在typings.d.ts文件中声明它们,以便识别它们。

declare const some_variable: string

You can always have a web service that can serve you these variables through api calls as well.您始终可以拥有一个 Web 服务,它也可以通过 api 调用为您提供这些变量。

  1. Have an API that serves the config, and retrieve that from angular有一个提供配置的 API,并从 angular 中检索它

    app.get("/api/config/default", function(req, res) { res.send({ a: process.env["A_VAR"] }); })
  2. Have your back-end generate the angular constants让您的后端生成角度常数

    app.get("/generated-config.js", function(req, res) { res.send( "angular.module('myApp').constant('MY_CONFIG'," + "{'a': \\"" + process.env["A_VAR"] + "\\"" + "})" ); });

    For which you can use ng-constant .您可以使用ng-constant I like this approach better because blocking angular controllers and services that depend on this config becomes very easy.我更喜欢这种方法,因为阻止依赖于这个配置的 angular 控制器和服务变得非常容易。 In my case, the back-end generates this file and serves it together with the rest of the static files.就我而言,后端生成此文件并将其与其余静态文件一起提供。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Angular中将环境变量设置为常量 - Setting environment variables as constants in Angular 从 .env 设置 Angular 6 环境变量 - Set up Angular 6 environment variables from .env Angular 2环境变量-设置基本身份验证密码 - Angular 2 Environment Variables - Setting basic auth password Angular - 部署期间的生产环境变量 - Angular - Production environment variables during deployment 将 Angular 环境变量传递给已编译的 static angular 文件在 Z2A2D595E6ED9A0B234F027F2B66B 中的文件 - Pass Angular environment variables to compiled static angular files in spring boot 在 Angular 中,如何在构建后手动编辑环境变量? - In Angular, how to manually edit environment variables after build? 如何将 Angular 环境变量传递给 Gitlab CI/CD 管道 - How to pass Angular environment variables to Gitlab CI/CD Pipeline 在 QML 或 JavaScript 中调用操作系统环境 - Calling OS Environment in QML or JavaScript 使用Yeoman的全角堆栈生成器时,如何访问前端的环境变量? - How do you access environment variables on the front end when using Yeoman's angular-fullstack generator? angular2-starter配置文件(带有环境(测试,生产)变量,也称为.env或.conf) - angular2-starter config file (with environment (testing, production) variables aka .env or .conf)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM