问题描述
在Angular(类型脚本)中,有许多配置文件.保存全局设置的正确方法是哪一种?
In Angular (Type Script) there are many config files. Which is the right one to save a global setting?
例如,当我从本地调用API时,我的rootUrl
是localhost:42000
,但是当我打开生产时,它应该是http:www.someting.com
.
For example, when I am calling API from local then my rootUrl
is localhost:42000
but when I switch on production it should be http:www.someting.com
.
我想将此rootUrl
保存在某个全局位置,因此,如果我开始生产,则只需要更改此rootUrl
.
I want to save this rootUrl
in some global place so if I switch on production then I only have to change in this rootUrl
.
请建议将这些全局设置保存在哪里,与Asp.Net中的web.config相同.
Please suggest where should I save these global settings, same as web.config in Asp.Net.
推荐答案
此答案与@trichetriche相似,但在代码上没有更多详细信息.
This answer is similar to @trichetriche, with few more details on the code.
用于development/testing
目的
environment.ts
environment.ts
export const environment = {
production: false,
appUrl: 'localhost:4200'
};
对于production
environment.prod.ts
environment.prod.ts
export const environment = {
production: true,
appUrl: 'mywebsite.com'
};
用法
service.ts
service.ts
import { environment } from '../../environments/environment';
this._http.get(environment.appUrl, requestHeaders(options));
在所有environment
文件中记下production
参数.我相信您还可以创建更多的环境,例如environment.unittesting.ts
.
Make a note of production
parameter in all the environment
files.I believe you could also create more environments like environment.unittesting.ts
.
这篇关于在Angular 4中将全局设置保存到何处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!