我试图从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。
当前回答
再次讨论这个问题,因为我的解决方案没有列在这里。
我正在用rxjs 6.0运行Angular 6,遇到了这个错误。
下面是我解决问题的方法:
我改变了
map((response: any) => response.json())
简单地成为:
.pipe(map((response: any) => response.json()));
我在这里找到了解决方法:
https://github.com/angular/angular/issues/15548#issuecomment-387009186
其他回答
我也曾犯过同样的错误。一个对我有效的解决方案是在您的服务中添加以下行。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 6.0运行Angular 6,遇到了这个错误。
下面是我解决问题的方法:
我改变了
map((response: any) => response.json())
简单地成为:
.pipe(map((response: any) => response.json()));
我在这里找到了解决方法:
https://github.com/angular/angular/issues/15548#issuecomment-387009186
只需安装rxjs-compat就可以解决这个问题
npm i rxjs-compat --save-dev
然后像下面这样导入它
import 'rxjs/Rx';
在Angular v10中。X和RXJS v6.x
第一次导入我的服务地图顶部,
Import {map} from 'rxjs/operators';
然后我用这样的地图
return this.http.get<return type>(URL)
.pipe(map(x => {
// here return your pattern
return x.foo;
}));
import { map } from "rxjs/operators";
getGetFunction(){
this.http.get('http://someapi')
.pipe(map(res => res));
}
getPostFunction(yourPara){
this.http.get('http://someapi',yourPara)
.pipe(map(res => res));
}
在上面的函数中,你可以看到我没有使用res.json(),因为我使用HttpClient。它自动应用res.json()并返回Observable (HttpResponse < string>)。在HttpClient中使用angular 4之后,你不再需要自己调用这个函数。