简体   繁体   English

使用Angular frondend从多个服务器获取数据

[英]Fetching data from multiple servers with Angular frondend

We are developing an ERP with multiple back-ends running on various servers, for example, hr at abc.com/hr/api finance at xyz.com/api and so on 我们正在开发一个在各种服务器上运行多个后端的ERP,例如,在xyz.com/api上的abc.com/hr/api finance,等等。

But we have single front-end Angular 6 application, we may be fetching data from various servers at a time. 但是我们有单个前端Angular 6应用程序,我们可能一次从各种服务器获取数据。

I am new to Angular, but what little bit I know is we can define variables in environment.ts for either development , production or test server, but what If I bypass enviroment.ts settings with what ever http url I want. 我是Angular的新手,但我知道的是我们可以在environment.ts中为开发,生产或测试服务器定义变量,但是如果我绕过enviroment.ts设置我想要的http url。 Thanks 谢谢

Angular should not be concerned where it gets its Data from. Angular不应该关注从哪里获取数据。 You can contact multiple APIs. 您可以联系多个API。 You dont have to specify the api url youre targeting in the environment (it makes it easier if youre always targeting the same api). 您不必指定您在环境中定位的api url(如果您始终定位相同的API,则会更容易)。

Or you can define multiple Api urls in the environment under different names eg API_URL_HR="" API_URL_FINANCE="". 或者您可以在环境中以不同的名称定义多个Api URL,例如API_URL_HR =“”API_URL_FINANCE =“”。

The Http request needs to get the url from somewhere, but this can be an environment variable or a simple String or a variable. Http请求需要从某个地方获取url,但这可以是环境变量或简单的String或变量。

I am not a pro either in Angular. 我不是Angular的专业人士。 Have been working on Angular from 6 months now. 从6个月开始一直在研究Angular。

So upto my knowledge, the preferred practice is to declare the Base Url in the environment.ts. 据我所知,首选的做法是在environment.ts中声明Base Url。 When you BUILD the angular project it only takes the PROD environment.ts. 当您构建角度项目时,它只需要PROD环境.ts。

Now, coming to your question "but what If I bypass enviroment.ts settings with what ever http url I want." 现在,提出你的问题“但如果我绕过enviroment.ts设置我想要的http url。” What I understand is if you don't define the base url in the environment.ts file you'll have to declare it in any of your component, that kind of works but if you someday have to change your Base url you'll have to change the link from each and every component you are using it in. So better stick with environment.ts. 我的理解是,如果你没有在environment.ts文件中定义基本网址,你必须在你的任何组件中声明它,这种工作,但如果你有一天必须更改你的基本网址,你将拥有更改您正在使用它的每个组件的链接。所以最好坚持使用environment.ts。

I hope that answers your question rightly. 我希望能正确回答你的问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM