代码之家  ›  专栏  ›  技术社区  ›  nick gowdy

“rxjs”可见。投掷不是函数-角度4

  •  43
  • nick gowdy  · 技术社区  · 7 年前

    我一直在学习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
    
    5 回复  |  直到 5 年前
        1
  •  126
  •   Dharman Fitopawer    4 年前

    错误 There are multiple modules with names that only differ in casing. Observable .

    导入应使用大写字母“O”,如:

    import { Observable } from 'rxjs/Observable';

    可观察的 catch throw 在创建的观察值上。

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

    要导入完整的可观察对象,请按如下方式导入:

    import { Observable } from 'rxjs/Rx'

    map() filter() 可以用作 pipeable operators pipe()

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

    操作员导入为:

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

    import { throwError } from 'rxjs';
    

    然后抛出如下错误:

    if (error.status === 404)
        return throwError( new NotFoundError(error) )
    
        2
  •  29
  •   prijesh bhingradiya    5 年前

    在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

        3
  •  13
  •   Sibeesh Venu    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) .

    onError(res: Response) {
        const statusCode = res.status;
        const body = res.json();
        const error = {
          statusCode: statusCode,
          error: body.error
        };
        return throwError(error);
      }
    
        4
  •  4
  •   Waleed Shahzaib    5 年前

    _在RxJS的较新版本中,throw已被丢弃
    对于较新版本的RxJS(6+),请使用以下选项:

     import { throwError } from 'rxjs'; 

     if (error.status === 404)
        return throwError( new NotFoundError(error) )
    
        5
  •  0
  •   Rob Lassche    4 年前

    角度9可见:

    1. 如果数据到达且状态正常,则发送数据
    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 ) ;
            }   
          }),
        )   
    }