我想从配置文件加载所有网址。我创建了名为'config.json'的json文件,ConfigurationService.ts来按键获取URL。但我无法按键获取URL。
config.json:
{
"loginUrl": "http:example/login",
"verifyUrl": "http:example/verify"
}
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的一部分:
private x =this.configurationService.getConfiguration("verifyUrl").subscribe((result) => console.log(result));
我如何仅收到loginUrl?
答案 0 :(得分:5)
我认为您正在寻找的是:
getConfiguration(key) {
return this.http.get('./app/config/config.json').map(res => {
this.result = res.json();
return this.result[key];
});
}