简体   繁体   English

Angular 8:使用环境文件维护API URL

[英]Angular 8: Using Environment file for maintaining API URL

I am using Angular8 for completely new project. 我正在将Angular8用于全新项目。 I ran into a strange issue. 我遇到了一个奇怪的问题。 When I deployed production build on my server, The domain name in the URL gets duplicated. 当我在服务器上部署生产版本时,URL中的域名会重复。 for example, https://my.domain.com/api becomes https://my.domain.com/my.domain.com/api My environment.prod.ts looks like this 例如, https://my.domain.com/api变为https://my.domain.com/my.domain.com/api我的environment.prod.ts看起来像这样

export const environment = {
  production: true,
  apiUrl: 'https:/my.domain.com/api/',
};

I use the URL in my code as: 我在代码中将URL用作:

myApiUrl: string = environment.apiUrl + 'v1/services/users;

What I get is: 我得到的是:

https://my.domain.com/my.domain.com/api/v1/services/users

Angular application is deployed on Tomcat 8.5 on Azure web apps as ROOT folder. Angular应用程序作为ROOT文件夹部署在Azure Web应用程序上的Tomcat 8.5上。

if your api is relative to domain, but not to app location like: 如果您的api是相对于域的,而不是相对于应用程序的位置,例如:

app: https://my.domain.com/app 应用: https://my.domain.com/apphttps://my.domain.com/app

api: https://my.domain.com/api api: https://my.domain.com/apihttps://my.domain.com/api

you can use apiUrl: location.origin + '/api/' , this way app will work no matter where will be hosted 您可以使用apiUrl: location.origin + '/api/' ,这样,无论在哪里托管应用程序都可以正常工作

也许是错字https:/ single /

First of all, it was stupid mistake from my side where i missed one / in https:// 首先,这是我这边的愚蠢错误,我在https://中错过了一个/

However, As my api is on same domain as angular app, both answers to this thread (Damian and Patricio) also worked. 但是,由于我的api与angular应用程序位于同一个域中,因此对该线程的两个答案(Damian和Patricio)也都有效。 If API is on other domain, then just putting correct URL should work just fine. 如果API在其他域上,则只需输入正确的URL就可以了。

Your file should look like this 您的文件应如下所示

export const environment = {
  production: true,
  apiUrl: 'api/',
};

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

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