2016-10-04 181 views
0

我對如何執行http get call有點困惑。我app.service是像這樣:Angular 2 http calls

import { Injectable } from '@angular/core'; 
import { Http, Response } from '@angular/http'; 
import { Observable } from 'rxjs/Rx'; 

@Injectable() 
export class AppService { 
constructor(private http: Http){} 
    fetchData(){ 
    return this.http.get('http://date.jsontest.com/').map(
     (res) = > res.json() 
     ).subscribe(
     (data) => console.log(data) 
    ); 
    } 
} 

這導致Cannot find name 'res'錯誤。

我的app.module有HttpModule。

通過查看Angular 2 doc on HTTP requests,他們有類似getHeroes(): Observable<Hero[]>。但我不確定import { Hero } from './hero';行中包含的是什麼。

回答

1

,應該是

(res) => res.json

+0

哇線

(res) = > res.json()

。我無望。儘管如此,修復後,我得到了另一個錯誤。這次它說'this.http.get(...)。map不是一個函數' – abrahamlinkedin

+0

明白了。然後我不得不導入'import'rxjs/Rx';' – abrahamlinkedin

+1

我剛剛說你只能從rxjs 導入map運算符,而不是整個rxjs庫 – Nico