Mam następujące usługi, które działa dobrze aż do dzisiaj mam ten błądAngular2 HTTP RXJS obserwowalnych Błąd typu:.. This.http.get (...) map (...) catch nie jest funkcją
TypeError: this.http.get(...).map(...).catch is not a function.
Kiedy i, debugowanie tego kodu powoduje awarię, jeśli chodzi o metodę catch.
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);
}
}
Zadziałało! dzięki. to szalone, że mam 'import 'rxjs/Rx' w moim pliku dostawców, gdzie importuję moje inne zależności również dla angular2, ale uważam, że muszę zaimportować tę bibliotekę do każdego pliku, w którym chcę go użyć lub w głównej. ts. –
@Thierry, najwyraźniej importowanie całego rxjs/Rx nie jest dobrą praktyką. Czy uważasz, że importowanie tylko tych operatorów, których potrzebujesz, nie jest tak wielką poprawą? –