rxjs__WEBPACK_IMPORTED_MODULE_3__.Observable.throw is not a function at

六眼飞鱼酱① 提交于 2019-12-08 22:01:08

问题


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?


回答1:


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

Do it this way instead,

import { throwError } from 'rxjs';

throwError(error.message || error);

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.

Hope this helps.



来源:https://stackoverflow.com/questions/52440245/rxjs-webpack-imported-module-3-observable-throw-is-not-a-function-at

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!