我有一个配置服务,用于从json文件中检索特定的信息。Angular 2获得系列请求
getConfiguration(key) {
return this.http.get('./app/config/development.json').map(res => {
this.result = res.json();
return this.result[key];
});
}
我试图让我的api(在development.json)的基础网址,并使用此网址发出请求。
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()'是,但不输出任何可以订阅的东西。下面的回答将帮助你解决其余的问题,但她错过了回报以及目前的状况。 –
我不太明白,方法“getjoueurs”正在返回可观察对象 return this.http.get(url +' joueur')。map(res => res = res.json()); 这行应该返回我可观察的对象。 你能帮我弄清楚丹尼斯吗? 有效地在朱莉娅的回答中,getJoueurs方法中没有返回任何内容。 – user3668100