我试图从Angular调用一个API,但得到这个错误:
属性“map”在类型“Observable<Response>”上不存在
这个类似问题的答案并没有解决我的问题:Angular 2 beta版。17:属性“map”在类型“Observable<Response>”上不存在。
我使用的是Angular 2.0.0-beta.17。
我试图从Angular调用一个API,但得到这个错误:
属性“map”在类型“Observable<Response>”上不存在
这个类似问题的答案并没有解决我的问题:Angular 2 beta版。17:属性“map”在类型“Observable<Response>”上不存在。
我使用的是Angular 2.0.0-beta.17。
当前回答
如果你使用这个旧的方法来获取路由参数
this._route.params
.map(params => params['id'])
为新的rxjs版本更新它
首先, 从RXJS操作符中导入映射。
import { map } from 'rxjs/operators';
第二个添加管道,
this._route.params.pipe(
map(params => params['id']))
其他回答
如果你使用这个旧的方法来获取路由参数
this._route.params
.map(params => params['id'])
为新的rxjs版本更新它
首先, 从RXJS操作符中导入映射。
import { map } from 'rxjs/operators';
第二个添加管道,
this._route.params.pipe(
map(params => params['id']))
首先运行安装,如下所示:
npm install --save rxjs-compat@6
现在你需要在service.ts中导入rxjs:
import 'rxjs/Rx';
瞧!问题已经解决了。
角9:
forkJoin([
this.http.get().pipe(
catchError((error) => {
return of([]);
})
),
this.http.get().pipe(
catchError((error) => {
return of([]);
})
),
我也曾犯过同样的错误。一个对我有效的解决方案是在您的服务中添加以下行。Ts文件而不是导入'rxjs/add/operator/map':
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
举个例子,我的app。service。ts在调试之后,
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
@Injectable()
export class AppService {
constructor(private http: HttpClient) {}
getData(): Observable<any> {
return this.http.get('https://samples.openweathermap.org/data/2.5/history/city?q=Warren,OH&appid=b6907d289e10d714a6e88b30761fae22')
.pipe(map(result => result));
}
}
安装rxjs-compat只需在terminal中输入:
npm install --save rxjs-compat
然后导入:
import 'rxjs/Rx';