英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论