简体   繁体   English

角度动态环境API替换

[英]Angular Dynamic Environment API replacement

I have a simple application with login page. 我有一个简单的应用程序登录页面。 On this page there is login, password and extra dropdownlist field where user choose to which API he would like to connect. 在此页面上有登录名,密码和额外下拉列表字段,用户可以在其中选择要连接的API。

In code I have environments.ts file with apiEndpoint property and it's a string. 在代码中,我有带有apiEndpoint属性的environment.ts文件,它是一个字符串。

Example: 例:

export const environment: any = {
  settings: {
    apiEndpoint: 'http://example.com/API1'
  }
}

This apiEndpoint is used in all services in the application. 此apiEndpoint用于应用程序中的所有服务。

I would like to change this value in envitonments.ts dynamically. 我想动态地在envitonments.ts中更改此值。 So when the user choose on login page from dropdownlist ' API2 ' then environments.ts looks like: 因此,当用户从下拉列表' API2 '中选择登录页面时,environment.ts看起来像:

export const environment: any = {
  settings: {
    apiEndpoint: 'http://example.com/API2'
  }
}

your environment should be like this: 你的环境应该是这样的:

export const environment = {
   production: false,
   API1: 'https://example.com/api1',
   API2: 'https://example.com/api2'
};

and when you choosing an option from your select box call one of these APIs. 当您从选择框中选择一个选项时,请调用其中一个API。

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

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