我试图从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-compat就可以解决这个问题
npm i rxjs-compat --save-dev
然后像下面这样导入它
import 'rxjs/Rx';
其他回答
在rxjs 6中,map操作符的用法已经改变 现在你需要像这样使用它。
import { map } from 'rxjs/operators';
myObservable
.pipe(map(data => data * 2))
.subscribe(...);
供参考 https://www.academind.com/learn/javascript/rxjs-6-what-changed/#operators-update-path
我使用的是Angular 5.2,当我使用import 'rxjs/Rx';它抛出我以下lint错误: TSLint:此导入被列入黑名单,改为导入子模块(import-blacklist)
请看下面的截图:
解决方案: 通过只导入我需要的操作符解决了这个问题。例子如下:
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
因此,解决办法是只导入必要的操作符。
对于Angular 7v
改变
import 'rxjs/add/operator/map';
To
import { map } from "rxjs/operators";
And
return this.http.get('http://localhost/ionicapis/public/api/products')
.pipe(map(res => res.json()));
感谢https://github.com/nagamallabhanu
https://github.com/webmaxru/pwa-workshop-angular/issues/2#issuecomment-395024755
封装
管(地图(…))
工作
import { map } from 'rxjs/operators';
courseRef: AngularFireList<any>;
courses$: Observable<any[]>;
this.courseRef = db.list('/tags');
this.courses$ = this.courseRef.snapshotChanges()
.pipe(map(changes => {
return changes.map(c => ({ key: c.payload.key, ...c.payload.val()
}));
}));
this.courses$.subscribe(res=>{
console.log(res)
})
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之后,你不再需要自己调用这个函数。