rxjs__WEBPACK_IMPORTED_MODULE_3 __.Observable.throw不是函数 [英] rxjs__WEBPACK_IMPORTED_MODULE_3__.Observable.throw is not a function at

查看:83
本文介绍了rxjs__WEBPACK_IMPORTED_MODULE_3 __.Observable.throw不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做休息操作并抓住错误,然后执行以下操作:

I need to do rest operation adn catch erro and I do this:

import { Injectable,  } from '@angular/core';
import { Http, Response,Headers} from '@angular/http';

import { map} from 'rxjs/operators';
import { catchError } from 'rxjs/operators';
import { Observable} from 'rxjs';


@Injectable()
export class RESTSERCIVE{

 getObject(id: number){
    return this.http.get(this.url+"/"+id).pipe(map(
      (response: Response)=>{return response.json()},
      ),
      catchError(this.handleErrorObservable)
      );
  }
 handleErrorObservable (error: Response | any) 
  {   
      return Observable.throw(error.message || error);
  }
}

我不为什么会给我这个错误.有人可以帮助我吗?

I don't why it give me this error. Anyone can help me?

推荐答案

由于您使用的是Angular 6和RxJS 6,因此抛出错误的语法已更改.

Because you are using Angular 6 and RxJS 6, the syntax for throwing erros has changed.

用这种方式代替

import { throwError } from 'rxjs';

throwError(error.message || error);

在RxjS 6中,您也不需要将响应显式转换为JSON.它是自动完成的.因此,您可以从代码中删除地图.

Also in RxjS 6, you don't need to convert the response into JSON explicitly. It is done automatically. So you can remove the map from your code.

希望这会有所帮助.

这篇关于rxjs__WEBPACK_IMPORTED_MODULE_3 __.Observable.throw不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆