我刚刚掌握了Promise,现在我正在学习Observables!唉!
尝试使用Angular 4进行简单的get请求。轻松地遵循本教程:https://scotch.io/tutorials/angular-2-http-requests-with-observables。
最初,当我尝试从Observable
导入rxjs/Observable
时,我收到了一个编译错误,主要是说map不是Observable<Response>
的函数,而是将导入更改为rxjs
解决了编译错误。但是现在我在运行时遇到了同样的错误......
我如何处理以下回复有什么问题?
import { Injectable } from '@angular/core';
import { apiConfig } from '../../environments/environment';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { APIRequest, APIResponse} from './api-objects';
import { Observable } from 'rxjs';
/**
* The BaseAPIService can be extended to provide base functionality for communicating to the API endpoint.
*/
@Injectable()
export class BaseAPIService {
private apiBase = apiConfig.base;
constructor(protected http: Http) { }
/**
* Sends an API request to the endpoint
*/
sendRequest(request: APIRequest): Observable<APIResponse> {
const options = new RequestOptions({ });
const fullUrl = `apiBase${request.url}`;
request.body = request.body || {};
return this.http.get(fullUrl).map((response: Response) => new APIResponse(response));
}
}
**错误详情**
this.http.get(...).map is not a function
at CoreAPIService.webpackJsonp.356.BaseAPIService.sendRequest (base-api.service.ts:25)
答案 0 :(得分:0)
您还需要导入您计划使用的任何RxJS运算符。您可以在模块级别执行此操作。
示例:
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/switchMap';