文章内容

2020/5/25 17:19:21,作 者: 黄兵

“rxjs” observable.throw is not a function

最近在处理Angular Observable错误的的时候,出现了如下错误:

“rxjs” observable.throw is not a function

出现问题的原因:

由于版本变更,原来的写法已经不再使用。

具体代码如下:

private static handleErrors(error: any): Observable {
    const errors: string[] = [];
    let msg = '';

    msg = 'Status: ' + error.status;
    msg += ' - Status Text: ' + error.statusText;
    msg += ' - url: ' + error.url;
    msg += ' - Exception Message: ' + error.body.error;
    errors.push(msg);

    console.error('An error occurred', errors);

    return Observable.throw(errors);
  }

解决方案:

修改代码错误的抛出方式:

 import {Observable, throwError} from 'rxjs';
 
  private static handleErrors(error: any): Observable {
    const errors: string[] = [];
    let msg = '';

    msg = 'Status: ' + error.status;
    msg += ' - Status Text: ' + error.statusText;
    msg += ' - url: ' + error.url;
    msg += ' - Exception Message: ' + error.body.error;
    errors.push(msg);

    console.error('An error occurred', errors);

    return throwError(errors);
  }


参考资料:

1、“rxjs” observable.throw is not a function - Angular4


黄兵个人博客原创。

转载请注明出处:黄兵个人博客 - “rxjs” observable.throw is not a function

分享到:

发表评论

评论列表