在Angular 2中从config.json获取数据 [英] Get data from config.json in Angular 2
本文介绍了在Angular 2中从config.json获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从配置文件中加载所有网址.我已经创建了名为'config.json的json文件ConfigurationService.ts来通过密钥获取URL.但是我无法通过密钥获取URL.
I want to load all url from a config file. I've created json file named 'config.json", ConfigurationService.ts to get the URL by key. But I cannot get the URL by key.
config.json:
config.json:
{
"loginUrl": "http:example/login",
"verifyUrl": "http:example/verify"
}
ConfigurationService.ts:
ConfigurationService.ts:
import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Headers, RequestOptions} from '@angular/http';
import {Observable} from 'rxjs/Observable';
@Injectable()
export class ConfigurationService {
constructor(private http:Http) {}
private result: Object;
getConfiguration(key) {
return this.http.get('./app/config/config.json').map(res => {
res.json();
this.result = res._body;
return this.result[key]; //returns 'undefined', but when I return only 'this.result' it shows me all json data {...}
});
}
}
auth_service.ts的一部分:
part of auth_service.ts:
private x =this.configurationService.getConfiguration("verifyUrl").subscribe((result) => console.log(result));
例如,我如何仅接收loginUrl?
How do I receive only the loginUrl for example?
推荐答案
我认为您正在寻找的是这个
I think what you're looking for is this:
getConfiguration(key) {
return this.http.get('./app/config/config.json').map(res => {
this.result = res.json();
return this.result[key];
});
}
这篇关于在Angular 2中从config.json获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文