我试图从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']))

其他回答

我使用的是Angular 5.2,当我使用import 'rxjs/Rx';它抛出我以下lint错误: TSLint:此导入被列入黑名单,改为导入子模块(import-blacklist)

请看下面的截图:

解决方案: 通过只导入我需要的操作符解决了这个问题。例子如下:

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';

因此,解决办法是只导入必要的操作符。

我试了上面所有可能的答案,没有一个成功,

我通过简单地重新启动我的IDE,即Visual Studio Code来解决它

希望它能帮助到别人。

角9:

 forkJoin([
  this.http.get().pipe(
    catchError((error) => {
      return of([]);
    })
  ),
  this.http.get().pipe(
    catchError((error) => {
      return of([]);
    })
  ),

在最新的Angular 7中。,你可以简单地尝试:

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);
      })
    );
  }

查看这个演示以获得更多细节。

在我的情况下,只包括地图和承诺是不够的:

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';

我按照官方文档的建议导入了几个rxjs组件,解决了这个问题:

1)在一个app/rxjs-operators中导入语句。ts文件:

// import 'rxjs/Rx'; // adds ALL RxJS statics & operators to Observable

// See node_module/rxjs/Rxjs.js
// Import just the rxjs statics and operators we need for THIS app.

// Statics
import 'rxjs/add/observable/throw';

// Operators
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/operator/toPromise';

2)在你的服务中导入rxjs-operator本身:

// Add the RxJS Observable operators we need in this app.
import './rxjs-operators';