Tôi có dịch vụ đang hoạt động tốt cho đến hôm nay tôi gặp lỗi nàyAngular2 Http với RXJS Loại lỗi quan sát được: this.http.get (...) Bản đồ (...). Catch không phải là chức năng
TypeError: this.http.get(...).map(...).catch is not a function.
Khi i, m gỡ lỗi mã này nó gặp sự cố khi nói đến phương pháp bắt.
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);
}
}
Nó hoạt động! cảm ơn. Thật điên rồ khi tôi có tập tin «import» rxjs/Rx' trong tập tin nhà cung cấp nơi tôi nhập các phụ thuộc khác của tôi cũng cho angular2, nhưng tôi tin rằng tôi cần phải nhập thư viện này vào mỗi tập tin mà tôi muốn sử dụng hoặc trong chính. ts. –
@Thierry, dường như việc nhập toàn bộ rxjs/Rx là không tốt. Bạn có nghĩ rằng đó không phải là một cải tiến để chỉ nhập các toán tử mà bạn cần? –