以下是我的service.ts
。我添加了map
并单独捕获。 observable<response>
import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable'; // for handling service response - like promise
import { DashboardModel } from '../model/dashboard.model'
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';// Import RxJs required methods
@Injectable()
export class AppService {
constructor(private http: Http) { }
// private instance variable to hold base url
private baseUrl = 'https://www.google.co.in';
// Fetch all existing comments
getDetails(): Observable<DashboardModel[]> {
// ...using get request
return this.http.get(this.baseUrl)
// ...and calling .json() on the response to return data
.map((res: Response) => res.json())
//...errors if any
.catch((error: any) => Observable.throw(error.json().error || 'Server error'));
}
}
编辑:我已经看到了解决方案,在哪里添加map,catch等要单独导入。即便如此,我也会遇到同样的错误。