2017-05-01 31 views
0

我有一个配置服务,用于从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存在”。我在这里做错了什么,以及什么是串联请求的正确方法。

+0

你得到“void”错误的原因是你的'getJoueurs()'函数不返回任何东西。它运行'getConfiguration()'是,但不输出任何可以订阅的东西。下面的回答将帮助你解决其余的问题,但她错过了回报以及目前的状况。 –

+0

我不太明白,方法“getjoueurs”正在返回可观察对象 return this.http.get(url +' joueur')。map(res => res = res.json()); 这行应该返回我可观察的对象。 你能帮我弄清楚丹尼斯吗? 有效地在朱莉娅的回答中,getJoueurs方法中没有返回任何内容。 – user3668100

回答

0

好了我要去回答一下就能解释一下。 @ julia的答案大多存在,但它仍然不适合你的功能。

我的猜测是你的行:this.requestService.getJoueurs().subscribe((joueurs) => console.log(joueurs));是你的实际使用案例的替身,所以我将解决它与Julia的注销函数本身的方法。 (完全有效)

你可以看到打字稿和类型你的订阅正在发生什么。你在getJoueurs()函数中没有返回任何东西。

var myPhrase = "Do I show up?"; 

console.log(myPhrase); 
console.log('no:', noIDont()); 
console.log('yes:', yesIDo()); 

function noIDont() { 
    myPhrase = 'oh no'; 
} 

function yesIDo() { 
    myPhrase = 'Yay!'; 
    return myPhrase; 
} 

// You will see: 
// Do I show up? 
// No: 
// Yes: Yay! 

你的功能需要返回

如果我们对你的函数打字稿更多的铁杆,你可以写这些例子为:

function noIDont(): void { 
     myPhrase = 'oh no'; 
    } 

    function yesIDo(): string { 
     myPhrase = 'Yay!'; 
     return myPhrase; 
    } 

因此修改整个块包括朱莉娅的例子:

 // Get Config 
getConfiguration(key): Observable<any>{ 
    return this.http.get('./app/config/development.json').map(res => { 
     this.result = res.json(); 
     return this.result[key]; 
    }); 
    } 

// Get Joueurs, notice the type for the return 
getJoueurs(): Observable<any> { 
//▼▼▼▼▼▼ MUST HAVE 
    return this.conf.getConfiguration('apiBaseUrl') 
    .switchMap((url: any) => this.http.get(url + 'joueur')) 
           .map(res => res = res.json() 
    ); 
} 



// calling script in the component: 
this.requestService.getJoueurs().subscribe((joueurs) => console.log(joueurs)); 

所以呀,Julia的switchMapflatMap的伟大工程,只是一定要在你的functions.the返回值subscribe "does not exist on type void"错误是因为你正在返回null当你需要返回Observable<T>

希望帮助!

1

需要使用switchMap/mergeMap/concatMap操作:

这里是switchMap我主要使用:

项目每个源值到可观察其在输出合并观察的,发光值只能从最近预测的Observable中获得。 http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html

,如果你有AngularJS expireence并希望开始使用Observales你可以看看我的文章: $ Q地图RxJShttps://medium.com/@juliapassynkova/q-map-to-rxjs-981936a2b22d

getJoueurs() { 
    this.conf.getConfiguration('apiBaseUrl') 
    .switchMap((url: any) => this.http.get(url + 'joueur')) 
           .map(res => res = res.json() 
    ) 
    .subscribe(joueurs => console.log(joueurs)); 
} 
+0

不要忘了返回结果,你的代码现在只执行并且'getJoueurs()'不返回任何东西..这就是void的原因。 –

+0

原始结果是做.subscribe((joueurs)=>的console.log(joueurs));. –

+0

感谢您的回答朱莉娅,但我没有得到任何结果在这种情况下使用switchMap。 – user3668100