[英]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 调用为您提供这些变量。
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"] }); })
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.