2017-05-01 36 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