【发布时间】:2017-05-01 01:00:13
【问题描述】:
我有一个配置服务,用于从 json 文件中检索特定信息。
getConfiguration(key) {
return this.http.get('./app/config/development.json').map(res => {
this.result = res.json();
return this.result[key];
});
}
我正在尝试获取我的 api 的基本 url(在 development.json 中),并使用此 url 发出请求。
getJoueurs() {
this.conf.getConfiguration('apiBaseUrl').subscribe((url: any) => {
return this.http.get(url + 'joueur').map(res => res = res.json());
});
}
所以,我订阅以获取我的配置,然后尝试返回一个可观察对象,以便在我的组件中捕获它。
在我的组件中:
this.requestService.getJoueurs().subscribe((joueurs) => console.log(joueurs));
事实是我在订阅时遇到错误“在类型 void 上不存在”。我在这里做错了什么,以及连续发出获取请求的正确方法是什么。
【问题讨论】:
-
您收到“void”错误的原因是您的
getJoueurs()函数没有返回任何内容。它运行getConfiguration()yes,但它不输出任何可以订阅的内容。朱莉娅在下面的回答将帮助您解决剩下的问题,但她暂时也错过了回报.. -
我不太明白,方法“getjoueurs”正在返回可观察对象 return this.http.get(url + 'joueur').map(res => res = res.json( ));这条线应该返回我的可观察对象。你能帮我弄清楚丹尼斯吗?实际上,在 julia 的回答中,getJoueurs 方法中没有返回任何内容。
标签: json angular http callback angular2-services