在Angular 4中将全局设置保存到何处 [英] Where to save global settings in Angular 4

查看:171
本文介绍了在Angular 4中将全局设置保存到何处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Angular(类型脚本)中,有许多配置文件.保存全局设置的正确方法是哪一种?

In Angular (Type Script) there are many config files. Which is the right one to save a global setting?

例如,当我从本地调用API时,我的rootUrllocalhost: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中将全局设置保存到何处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆