Ho seguente servizio, che stava lavorando bene fino ad oggi ho ottenuto questo erroreAngular2 HTTP con RXJS TypeError Observable:.. This.http.get (...) Mappa (...) cattura non è una funzione
TypeError: this.http.get(...).map(...).catch is not a function.
Quando eseguo il debug di questo codice, si blocca quando si tratta di rilevare il metodo.
import { Test } from "./home.component";
import { Injectable } from "@angular/core";
import { Inject } from "@angular/core";
import { Http , Response } from "@angular/http";
import { Observable } from "rxjs/Observable";
@Injectable()
export class HomeService {
public constructor(@Inject(Http) private http: Http) {}
public getData(): Observable<Test []> {
return this.http.get("./src/app/home/home-data.json")
.map(this.extractData).catch(this.handleError);
}
public extractData(res: Response) {
let body = res.json();
return body.data || { };
}
public handleError (error: any) {
// In a real world app, we might use a remote logging infrastructure
// We"d also dig deeper into the error to get a better message
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : "Server error";
console.error(errMsg); // log to console instead
return Observable.throw(errMsg);
}
}
Ha funzionato! Grazie. è pazzesco che abbia questo 'import' rxjs/Rx' nel mio file dei fornitori dove importare le mie altre dipendenze anche per angular2, ma credo di aver bisogno di importare questa libreria in ogni file dove voglio usare questo o nel main. ts. –
@Thierry, apparentemente l'importazione dell'intero rxjs/Rx non è una buona pratica. Pensi che non sia tanto un miglioramento importare solo gli operatori di cui hai bisogno? –