从Angular 2中的config.json获取数据
我想从配置文件加载所有url。我已经创建了名为'config.json',ConfigurationService.ts的json文件来按键获取URL。但是我无法按键获取URL config.json:从Angular 2中的config.json获取数据,angular,Angular,我想从配置文件加载所有url。我已经创建了名为'config.json',ConfigurationService.ts的json文件来按键获取URL。但是我无法按键获取URL config.json: { "loginUrl": "http:example/login", "verifyUrl": "http:example/verify" } ConfigurationService.ts: import {Injectable} from '@angular/core'; import
{
"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 {...}
});
}
}
private x =this.configurationService.getConfiguration("verifyUrl").subscribe((result) => console.log(result));
auth_service.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 {...}
});
}
}
private x =this.configurationService.getConfiguration("verifyUrl").subscribe((result) => console.log(result));
例如,我如何只接收loginUrl?我想您需要的是:
getConfiguration(键){
返回这个.http.get('./app/config/config.json').map(res=>{
this.result=res.json();
返回此。结果[键];
});
}