1

值傳送我想創建一個可觀察其
1.獲取令牌getToken2方法
2.使用此令牌來獲得用戶數據與flatMap
3.分配剛剛收到用戶數據和令牌(這是通過flatMap收到)到localStorage
問題是我無法訪問第二個映射方法中的令牌。
那麼我怎樣才能在流中傳遞這個令牌值,以便我可以訪問它。RxJS時:物流

getCurrentUser2() { 
     return this.getToken2() 
     .flatMap((token) => this.http.get(this.URL + '/ROS/profile?token=' + token)) 
     .map((res) => res.json().data) 
     .map((response) => { 

      localStorage.setItem('currentUser', JSON.stringify({ 
       token:token, ,**want to access token value here** 
       userProfile: response 
      })); 
      localStorage.setItem('orgId', response.structure.id); 
      return this.toUser(response, '12'); 

     }); 
    } 

請給我想法來解決這個問題。
此致

回答

2

flatmapmergeMap具有稱爲resultSelector的可選參數。

根據源(外部)發射和內部可觀測發射的值和指數在輸出Observable上產生值的函數。傳遞給這個函數的參數是:

outerValue:從源
innerValue傳來值:從附帶的價值預計可觀察
outerIndex:從源
傳來價值的「指標」 innerIndex:從該投影可觀測

return this.getToken2() 
     .flatMap((token) => this.http.get(this.URL + '/ROS/profile?token=' + token),(token,response,outerIndex,innerIndex) => [token,response.json().data])) 
     .map((response) => { 
     //response is an array with both outer output and inner output. 
      localStorage.setItem('currentUser', JSON.stringify({ 
       token:response[0], 
       userProfile: response[1] 
      })); 
      localStorage.setItem('orgId', response[1].structure.id); 
      return this.toUser(response[1], '12'); 

     }); 
+0

價值的「指標」謝謝你這麼多偉大的解決方案。它真的幫了大忙! –