如何在Angular项目的environment.ts文件中引用其他环境属性?

huangapple go评论83阅读模式
英文:

How to reference other environment properties inside of environment.ts file in Angular project?

问题

我正在尝试修改 Angular 项目中的 'environment.ts' 文件并添加一些附加属性。我有以下内容:

export const environment = {
  production: false,
  apiUrl: 'http://example.com',
  authApiUrl: 'http://example.com/auth'
};

问题是是否可以以某种方式统一它并引用其他属性,使其看起来像这样:

export const environment = {
  production: false,
  apiUrl: 'http://example.com',
  authApiUrl: apiUrl + '/auth'
};
英文:

I'm trying to modify the 'environment.ts' file in Angular project and add some additional properties to it. What I have is:

export const environment = {
  production: false,
  apiUrl: 'http://example.com',
  authApiUrl: 'http://example.com/auth'
};

The question is if it's possible to somehow unify it and reference other properties so that it looked something like:

export const environment = {
  production: false,
  apiUrl: 'http://example.com',
  authApiUrl: apiUrl + '/auth'
};

答案1

得分: 3

考虑使用常量来处理这种类型的情况。像下面这样。

const apiBase = 'http://example.com';

export const environment = {
  production: false,
  apiUrl: apiBase,
  authApiUrl: apiBase + '/auth'
};

应该能够得到您期望的结果。在我的CLI应用程序中,这对我有效,可以让代码更加干净。

英文:

Think about using a constant for this type of thing. Like below.

const apiBase = 'http://example.com';

export const environment = {
  production: false,
  apiUrl: apiBase,
  authApiUrl: apiBase + '/auth'
};

Should give you the results you expect. Works for me in my CLI applications to get more DRY.

huangapple
  • 本文由 发表于 2020年1月6日 20:37:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612269.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定