Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在多个环境中使用的angular4应用程序中编码URL的最佳实践是什么?_Angular_Angular Http - Fatal编程技术网

在多个环境中使用的angular4应用程序中编码URL的最佳实践是什么?

在多个环境中使用的angular4应用程序中编码URL的最佳实践是什么?,angular,angular-http,Angular,Angular Http,我有一个angular 4应用程序,我正试图找到一种最佳实践方法,来创建一个用于创建服务的解决方案,或者某种用于处理许多URL的解决方案,以便在开发和生产环境中发出http请求 我在这一点上的解决方案是这样的 import { Injectable } from '@angular/core'; /** * PathsService is like a properties file for all of the URL paths used in the application. */ @

我有一个angular 4应用程序,我正试图找到一种最佳实践方法,来创建一个用于创建服务的解决方案,或者某种用于处理许多URL的解决方案,以便在开发和生产环境中发出http请求

我在这一点上的解决方案是这样的

import { Injectable } from '@angular/core';
/**
 * PathsService is like a properties file for all of the URL paths used in the application.
 */
@Injectable()
export class PathsService {

  constructor() {}

  productionEnvironment:string = "https://myproductionenvironment.com"
  developmentEnvironment:string = "https://mydevelopmentenvironment.com"

  activeRoot:string = productionEnvironment;


  loginResource = "/loginresources"
  employeeResource = "/employeeresouces"

  public getLoginResources(): string {
    return this.activeRoot+this.loginResource;
  }

  public getEmployeeResource():string {
    return this.activeRoot+this.employeeResource;
  }




}

虽然我相信这会很好,但有一个小问题,因为我必须在从开发环境切换到生产环境时更改activeRoot。我想知道是否有更好的方法来做到这一点,所以我不必手动切换。我可以使用javascript获取url,解析它,并以这种方式在生产和开发环境之间切换,但似乎应该有一种更好、更具角度的方法来解决这个问题。如果您对这个问题有任何意见,我们将不胜感激。谢谢

使用angular cli,您将在编译时生成environment.dev.ts和environment.prod.ts

例如:

如果您使用的是Angular CLI,则有一个名为
environments
的文件夹。其中,您有
environment.ts
environment.prod.ts

在这两个文件中都有一个导出的对象。在这个对象中,您可以添加一个
let-apirl='您的URL。然后,在文件中,只导入第一个文件(
environment.ts

然后,在构建时,只需运行
ngbuild--prod
,在编译时,它将使用
environment.ts
,而不是使用
environment.prod.ts


这够清楚吗

如果您正在使用Angular CLI,您应该有两个文件,
src/environments/environment.ts
src/environments/environment.prod.ts
。在这些文件中,您可以为生产和开发目的分配环境变量。只需从“../environments/environment”导入{environment}
,您就可以完成所有设置。CLI将自动为您选择合适的环境。

您可以查看@this link耶!作为补充,您可以创建所需的任何环境文件(环境开发、登台等),并使用集成持续工具(或不使用)使用所需的环境文件构建。确切地说,构建时,只需使用
ng build--environment=anyEnv