我试图从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。
当前回答
你需要导入map操作符:
import 'rxjs/add/operator/map'
或者更一般地说:
import 'rxjs/Rx';
注意:对于RxJS 6.x版本。X和以上,你将必须使用可管道的操作符,如下面的代码片段所示:
import { map } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';
// ...
export class MyComponent {
constructor(private http: HttpClient) { }
getItems() {
this.http.get('https://example.com/api/items').pipe(map(data => {})).subscribe(result => {
console.log(result);
});
}
}
这是由于RxJS团队删除了对使用的支持 更多信息请参见RxJS更新日志中的重大更改。
更新日志如下:
operator:可管道操作符现在必须像这样从rxjs中导入:import {map, filter, switchMap} from 'rxjs/operators';没有深度进口。
其他回答
再次讨论这个问题,因为我的解决方案没有列在这里。
我正在用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只需在terminal中输入:
npm install --save rxjs-compat
然后导入:
import 'rxjs/Rx';
你需要导入map操作符:
import 'rxjs/add/operator/map'
或者更一般地说:
import 'rxjs/Rx';
注意:对于RxJS 6.x版本。X和以上,你将必须使用可管道的操作符,如下面的代码片段所示:
import { map } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';
// ...
export class MyComponent {
constructor(private http: HttpClient) { }
getItems() {
this.http.get('https://example.com/api/items').pipe(map(data => {})).subscribe(result => {
console.log(result);
});
}
}
这是由于RxJS团队删除了对使用的支持 更多信息请参见RxJS更新日志中的重大更改。
更新日志如下:
operator:可管道操作符现在必须像这样从rxjs中导入:import {map, filter, switchMap} from 'rxjs/operators';没有深度进口。
角9:
forkJoin([
this.http.get().pipe(
catchError((error) => {
return of([]);
})
),
this.http.get().pipe(
catchError((error) => {
return of([]);
})
),
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之后,你不再需要自己调用这个函数。