简体   繁体   English

如何在我的angular 2应用程序中从命令行传递后端api url?

[英]How to pass backend api url from command line in my angular 2 app?

I want to deploy my angular app to multiple amazon instances (more than 10). 我想将我的角度应用程序部署到多个亚马逊实例(超过10个)。 For each instance the backend api also changes. 对于每个实例,后端api也会更改。

I dont want to hardcode the api url in environment files and change the environment like below 我不想在环境文件中对api url进行硬编码并更改如下所示的环境

ng serve --environment=prod

I want to specify the backend url with ng serve like below 我想用ng serve指定后端网址,如下所示

ng serve --backendApiUrl=http://www.example.com/api.

is that possible? 那可能吗?

Not possible out of the box. 开箱即用

The first option would be to put your backend api url behind a load balancer, so if there is a change the config needs to updated in one plus; 第一种选择是将您的后端api url放在负载均衡器之后,因此,如果有更改,则需要在一个以上的时间内更新配置; plus you get the added benefit of load balancing. 另外,您还将获得负载平衡的额外好处。 Something like AWS Elastic Load Balancing or Netflix could help. 诸如AWS Elastic Load Balancing或Netflix之类的功能可能会有所帮助。

Assuming you can't do that, the ng serve command doesn't have the ability to do that ( ng serve source ). 假设您无法执行此操作,那么ng serve命令将无法执行此操作( ng serve source )。 So your best options would be to write a bash script that would take that as an argument, and store that for ng2 to use. 因此,最好的选择是编写一个bash脚本,将其作为参数,并将其存储以供ng2使用。 Obviously at this point your angular code is already bundled together, so there won't be a way to inject/weave into your code as you would be able to at build time with webpack, etc. But you could achieve this with a separate file that angular pulls into define the backend API. 显然,此时您的角度代码已经捆绑在一起,因此将无法像在webpack等构建时那样将代码注入/编织到您的代码中。但是您可以使用单独的文件来实现角度拉入定义了后端API。 Then your bash script would only need to overwrite this file to update the URL. 然后,您的bash脚本只需要覆盖此文件即可更新URL。

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

相关问题 如何在 angular 中从后端获取数据时传递 URL 中的变量? - How to pass variables in URL while getting data from backend in angular? 如何使用maven eirslett前端插件从我的Angular-cli应用程序调用不同的api域(后端)? - How to call different api domain(backend) from my Angular-cli app using maven eirslett frontend plugin? 如何将命令行参数传递给 angular 应用程序 - How to pass a command line argument to an angular application 如何将参数从 angular 传递到我的 nodejs API? - How do I pass parameters from angular into my nodejs API? 如何从 Angular 6 中的选定下拉列表将 Id 传递给我的 API 请求? - How to pass Id to my API request from a selected dropdown in Angular 6? 如何通过 id 获取 api url?(角度) - How to pass id to get api url?(Angular) 如何使用 API 在我的后端访问我的 angular 前端图像 - How to access to my angular frontend image in my backend with API 如何在开发期间为我的 angular 应用程序创建后端代理? - How to make a proxy for my backend for my angular app during the dev? 无法从我的 API 后端(角度)订阅数据 - Cannot subscribe data from my API backend (angular) 如何将数据从 Spring 后端传递到 Angular 前端? - How to pass data from Spring backend to Angular frontend?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM