繁体   English   中英

“rxjs” observable.throw 不是函数 - Angular4

[英]“rxjs” observable.throw is not a function - Angular4

我一直在学习 Angular 4,一切都很顺利,直到我尝试在服务中实现捕获处理。 我正在尝试使用“rxjs”捕获并抛出,但我的控制台中有一个未定义的函数错误。

import { Injectable } from '@angular/core';
import { Http } from "@angular/http";
import { Observable } from 'rxjs/observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import { AppError } from "../app/common/app.error";
import { NotFoundError } from "../app/common/not-found-error";
import { BadInput } from "../app/common/bad-input";

@Injectable()
export class PostService {
  private url = "https://jsonplaceholder.typicode.com/posts";

  constructor(private http: Http) { }

 deletepost(post){
      // return this.http.delete(this.url + '/' + post.id)
      // Hard-coded id to test 404
      return this.http.delete(this.url + '/' + 93498)
        .catch((error: Response) => {
          console.log('error within catch is ' + Response)
          if(error.status === 404)
            return Observable.throw(new NotFoundError(error));

          return Observable.throw(new AppError(error));
        });
    }
}

这是错误消息:

TypeError: __WEBPACK_IMPORTED_MODULE_2_rxjs_observable__["Observable"].throw is not a function. 
(In '__WEBPACK_IMPORTED_MODULE_2_rxjs_observable__["Observable"].throw(new 
__WEBPACK_IMPORTED_MODULE_6__app_common_not_found_error__["a" /* NotFoundError 
*/](error))', 
'__WEBPACK_IMPORTED_MODULE_2_rxjs_observable__["Observable"].throw' is 
undefined) — post.service.ts:42

我的浏览器中也有此警告:

./~/rxjs/Observable.js
There are multiple modules with names that only differ in casing.
This can lead to unexpected behavior when compiling on a filesystem with other case-semantic.
Use equal casing. Compare these module identifiers:
* /Users/nickgowdy/Desktop/Angular2/angular4 source code/hello-world/node_modules/rxjs/Observable.js
    Used by 14 module(s), i. e.
    /Users/nickgowdy/Desktop/Angular2/angular4 source code/hello-world/node_modules/@angular/core/@angular/core.es5.js
* /Users/nickgowdy/Desktop/Angular2/angular4 source code/hello-world/node_modules/rxjs/observable.js
    Used by 1 module(s), i. e.
    /Users/nickgowdy/Desktop/Angular2/angular4 source code/hello-world/node_modules/@ngtools/webpack/src/index.js!/Users/nickgowdy/Desktop/Angular2/angular4 source code/hello-world/src/services/post.service.ts

错误There are multiple modules with names that only differ in casing. 表明您尝试使用Observable方式是针对错误的导入。

导入应该是大写的“O”,如:

import { Observable } from 'rxjs/Observable';

这将导入单独的Observable运算符,该运算符与创建的 Observable 上的catchthrow等运算符结合使用。

import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';

要导入完整的 Observable 对象,您可以像这样导入它:

import { Observable } from 'rxjs/Rx'

更新:

使用较新版本的 RxJS (5.5+) 运算符,例如map()filter()可以用作管道运算符,与pipe()结合使用,而不是链接。 它们是进口的,例如:

import { filter, map, catchError } from 'rxjs/operators';

请记住,诸如throw类的术语在 JavaScript 中是保留字/关键字,因此 RxJS throw运算符被导入为:

import { _throw } from 'rxjs/observable/throw';

更新:

对于较新版本的 RxJS (6+),请使用:

import { throwError } from 'rxjs';

并抛出这样的错误:

if (error.status === 404)
    return throwError( new NotFoundError(error) )

在 RxJS 6 中, Observable.throw()throwError()取代,它的操作与其前身非常相似。 因此,您可以通过导入将Observable.throw(error)替换为仅throwError(error)

import { throwError } from 'rxjs';

查看此链接以获取进一步参考: https : //www.metaltoad.com/blog/angular-6-upgrading-api-calls-rxjs-6

我在angular 5应用程序中遇到了同样的问题。 我所做的是,添加一个新包。

import { throwError } from 'rxjs';
import { filter, map, catchError } from 'rxjs/operators';

从我的http服务调用中,我返回了一个函数。

return this.http.request(request)
      .pipe(map((res: Response) => res.json()),
        catchError((res: Response) => this.onError(res)));

onError函数中,我用throwError(error)返回throwError(error)

onError(res: Response) {
    const statusCode = res.status;
    const body = res.json();
    const error = {
      statusCode: statusCode,
      error: body.error
    };
    return throwError(error);
  }

_throw 已在较新版本的 RxJS 中被丢弃
对于较新版本的 RxJS (6+),请使用:

import { throwError } from 'rxjs';

并抛出这样的错误:

if (error.status === 404)
    return throwError( new NotFoundError(error) )

在 Angular9 Observable 中:

  1. 如果数据到达且状态正常,则发送数据
  2. 如果数据的 STATUS 不正常,则抛出错误
myObsFunc(): Observable<any> { 
  return this.http.get<any>('/api/something') 
    .pipe(
      /* Catch a backend error and let the component know */
      catchError( err => {
        /* Rethrow error */
        return throwError( err );
      }),
      map( (res)=> {
        if( res.STATUS == "OK" ) {
          /* Send DATA to subscriber */
          return Object.values( res.DATA)
        } else {
           /* Inform subscriber that a functional error occured */
           throw ( "getOrphans: Status is not OK but "+ res.STATUS ) ;
        }   
      }),
    )   
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM