简体   繁体   English

通过在Angular 6中设置环境变量来实现动态网址?

[英]Dynamic Url by setting environment variables in Angular 6?

I'm new to angular and trying to implement Dynamic API in it. 我是angular的新手,正在尝试在其中实现Dynamic API In a nutshell, Dynamic API means that based on the account that has been logged In, my app should call that API .So that means if I have logged in from Dev account (rajat-dev@xyz.com) it should call Dev API and if I'm logged In from PROD account (rajat-prod@xyz.com), it should call Prod API .After researching much, I have found out that you cannot use webpack in the newer ANGULAR CLI . 简而言之, Dynamic API意味着基于已登录的account ,我的应用应调用该API 。因此,这意味着如果我已从Dev帐户(rajat-dev@xyz.com)登录,则应调用Dev API如果我是从PROD帐户(rajat-prod@xyz.com)登录的,则应该调用Prod API 。经过大量研究,我发现您无法在较新的ANGULAR CLI中使用webpack But I can edit environment.ts . 但是我可以编辑environment.ts I have API's as https://admin-console.api.prod.xyz.io (prod) and https://admin-console.api.xyz.io (dev) respectively. 我的API分别为https://admin-console.api.prod.xyz.io (prod)https://admin-console.api.xyz.io (dev) How can I achieve that using from the environment variables?. 如何使用环境变量来实现这一目标?

Actually, you can implement your dynamic API without using the environment variables. 实际上,您可以在不使用环境变量的情况下实现动态API。 You just have to add an property to the account object so that you know which the current logged in account is type dev or prod . 您只需向帐户对象添加一个属性,以使您知道当前登录的帐户是dev还是prod 类型 After that, you just make a condition on that property and match with your APIs. 之后,您只需在该属性上设置一个条件并与您的API匹配即可。

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

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