我想从配置文件加载所有网址。我创建了名为“config.json”的 json 文件,ConfigurationService.ts 来通过密钥获取 URL。但是我无法通过密钥获取 URL。
配置.json:
{
"loginUrl": "http:example/login",
"verifyUrl": "http:example/verify"
}
配置服务.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?
我想你正在寻找的是这样的:
getConfiguration(key) {
return this.http.get('./app/config/config.json').map(res => {
this.result = res.json();
return this.result[key];
});
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)