我试图从Angular调用API,但出现以下错误:
属性“map”在类型“Observable<Response>”上不存在
这个类似问题的答案没有解决我的问题: Angular 2 beta.17: Property 'map' does not exist on type 'Observable<Response>'.
我正在使用Angular 2.0.0-beta.17版本。
我试图从Angular调用API,但出现以下错误:
属性“map”在类型“Observable<Response>”上不存在
这个类似问题的答案没有解决我的问题: Angular 2 beta.17: Property 'map' does not exist on type '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 的变更日志中的 breaking changes 。
从变更日志中得知:
operators:现在必须像这样从 rxjs 中导入可管道操作符:
import { map, filter, switchMap } from 'rxjs/operators';
。不再支持深度导入。
重新回顾这个问题,因为我的解决方案在这里没有列出。
我正在使用 Angular 6 和 rxjs 6.0,并遇到了这个错误。
以下是我解决它的方法:
我进行了更改。
map((response: any) => response.json())
只是存在
.pipe(map((response: any) => response.json()));
我在这里找到了解决方法:
https://github.com/angular/angular/issues/15548#issuecomment-387009186
import { map } from 'rxjs/operators';
。 - paul.catch
,我们可以使用catchError
,例如.pipe(catchError(this.handleError))
? - FindOutIslamNow只需在项目的VS Code终端中输入此命令并重新启动项目。
npm install rxjs-compat
您需要通过添加以下内容来导入map
运算符:
import 'rxjs/add/operator/map';
对于 Angular 7v
变更
import 'rxjs/add/operator/map';
import { map } from "rxjs/operators";
并且
return this.http.get('http://localhost/ionicapis/public/api/products')
.pipe(map(res => res.json()));
我在使用Angular 2.0.1时遇到了同样的问题,因为我从...导入了Observable
import { Observable } from 'rxjs/Observable';
我解决了从这个路径导入Observable的问题
import { Observable } from 'rxjs';
Observable
的所有运算符(包括您不使用的运算符)全部导入到捆绑包中,从而增加了捆绑包的大小。相反,您应该逐个导入每个运算符。我建议使用“可导入运算符”,它在 RxJS v5.5 中引入以支持更好的树摇优化。 - Sarun Intaralawanimport { map } from 'rxjs/operators';
myObservable
.pipe(map(data => data * 2))
.subscribe(...);
参考链接: https://www.academind.com/learn/javascript/rxjs-6-what-changed/#operators-update-path
RxJS v6
的任何人都需要使用 .pipe()
,否则没有运算符会直接从 Observable 中工作。您将看到一个错误,如“属性 'share' 不存在于类型 'Observable <{}>' 上”。 - atconway在 Angular 的新版 httpClient 模块中,你不需要再按照之前的方式编写:
return this.http.request(request)
.map((res: Response) => res.json());
但是要这样做:
return this.http.request(request)
.pipe(
map((res: any) => res.json())
);
import { Observable, of } from "rxjs";
import { map, catchError } from "rxjs/operators";
然后您可以按照以下方式使用这些方法
:
private getHtml(): Observable<any> {
return this.http.get("../../assets/test-data/preview.html").pipe(
map((res: any) => res.json()),
catchError(<T>(error: any, result?: T) => {
console.log(error);
return of(result as T);
})
);
}
Check this demo for more details.
只需在终端中键入以下命令即可安装rxjs-compat:
npm install --save rxjs-compat
然后导入:
import 'rxjs/Rx';
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>)。在Angular 4及更高版本中,您不再需要自己调用此功能。
npm install rxjs-compat
,然后导入map
操作符。 - Karan Raiyani