2016-03-26 126 views
0

Angular2 http請求返回observable。在observables的文檔中,您可以在observable上使用函數.map()Angular2 RxJS爲什麼map()沒有工作?

但是,當我在我的HTTP觀察到的使用.map()我得到這個錯誤:

argument is not a function. Are you looking for `mapTo()`? 

.mapTo()似乎提供了有點類似的行爲,但我不知道....爲什麼不.map()在這裏工作?

編輯:

謝謝,這裏有一些信息。我正在使用webpack和Angular 2.0.0-beta.8。

縮寫的實際代碼如下:

import { Observable, map, concatMap, flatMap, reduce, subscribe } from 'rxjs'; 

    getRequest(endpoint) { 
    return this.http.get('https://api.fitbit.com/1/user/-/' + endpoint, 
     { 
     headers: { 
      Authorization: 'Bearer ' + 
      localStorage.getItem(this.name + 'AccessToken'), 
      'Content-Type': 'application/json' 
     } 
     }).map(response => response.json()); 
    } 

    var profileRequest = this.getRequest('profile.json'); 
    var profileRequestLog = profileRequest.map('profile fetched'); 
+0

我是相當新的這個工作,所以不知道如果我可以提供幫助。你可以發表你如何提出你的http請求的樣本,也可能是你正在使用什麼版本的angular 2? –

+0

如果您已正確包含RxJS,則可以在可觀察IF上使用'.map()'。你有沒有這樣做?你確定它是正確加載和映射的嗎?顯示您的代碼和您的模塊配置將使我們更有可能指出您朝着正確的方向。 –

+0

你能否請添加一些代碼? – inoabrian

回答

2

請確保以下Rx.min.js文件包含在index.html

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/2.0.0-beta.9/Rx.min.js"> 
</script> //CDN reference 

然後你就可以導入以下與Observable及其operators

import {Observable} from 'rxjs/Observable'; 
import 'rxjs/Rx'; 
+0

我已經加入了一堆進口,但是所有這些進口都是必要的嗎?不應該包含相關的RxJS函數,比如'map()'? – benshope

+0

是rxjs會添加它,但你必須按照我的方式。導入rxjs/Rx將導入所有rx運算符。注意,如果你這樣做,你不需要調用/導入單個運營商。 – micronyks

相關問題