如何使用catchError()并在rxJs 6.0中仍返回类型化的Observable? [英] How do I use catchError() and still return a typed Observable with rxJs 6.0?

查看:140
本文介绍了如何使用catchError()并在rxJs 6.0中仍返回类型化的Observable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cd因此,我正在尝试将一些Angular 5代码迁移到6,并且我了解使用.pipe()运算符使rxjs正常工作所需的大多数更改.它可以像您期望的可点入"操作一样工作.

cdSo I'm trying to migrate some of my Angular 5 code to 6, and I understand most of the changes required for rxjs to work using the .pipe() operator. It works as you would expect for 'pipable' operations.

但是,catchError()的行为不同于.catch()运算符.在rxjs 6之前,我使用.catch()运算符将错误输入转换为一致的错误对象,然后可以在`.subscribe()中捕获该对象.

However, the behavior of catchError() is different than the .catch() operator. Prior to rxjs 6 I used the .catch() operator to transform the error input into a a consistent error object that can then be caught in the `.subscribe().

getAlbums(): Observable<Album[]> {
     return this.httpClient.get<Album[]>(this.config.urls.url("albums"))
           .map(albumList => this.albumList = albumList)
           .catch(new ErrorInfo().parseObservableResponseError);            
    }

new ErrorInfo().parseObservableResponseError是一个函数,该函数将错误对象作为输入,并将输入的错误解析为具有规范化对象的更简单的错误对象.捕获返回Observable<any>:

new ErrorInfo().parseObservableResponseError is a function that takes a error object as input and parses the input error into a simpler error object with a normalized object. Catch returns Observable<any>:

parseObservableResponseError(response): Observable<any> {
    let err = new ErrorInfo();
    ...
    return Observable.throw(err);        
}

这对于轻松处理rxjs5中的错误非常有用-错误基本上被捕获为流水线的一部分,然后抛出可以捕获的错误结构.subscribe()错误函数.

This worked great for easily handling errors up rxjs5 - errors basically are captured as part of the pipeline and then throw a well known error structure that can be captured .subscribe() error function.

但是将相同的代码移至rxjs 6并使用.pipe()我正在尝试执行以下操作:

Moving that same code to rxjs 6 however and using .pipe() I am trying to do the following:

getAlbums(): Observable<Album[]> {
    return this.httpClient.get<Album[]>(this.config.urls.url("albums"))
                .pipe(
                    map(albumList => this.albumList = albumList),
                    catchError(new ErrorInfo().parseObservableResponseError)                        
                );           
}

但是这不起作用,因为catchError现在将Observable<any>的结果返回到管道中,这不是我想要的.本质上,我只想像以前一样重新抛出任何错误.

However this doesn't work because catchError now returns a result of Observable<any> into the pipeline, which is not what I want here. In essence I just want to re-throw any errors as I did before.

错误TS2322:类型为'Observable< {} |不能将相册[]>"分配为可观察"类型

error TS2322: Type 'Observable<{} | Album[]>' is not assignable to type 'Observable'

如何模拟旧的运算符.catch()行为?

How do simulate the old operator .catch()behavior?

更新:

经过一番摸索之后,所示的代码开始工作,而没有给我一个构建错误.老实说,我不知道为什么它之前因错误消息而失败,但是现在可以正常工作了. @cartant在评论中的建议是一种明确指定结果的方法,并且效果很好.

After mucking around with this a bit more, the code as shown just started working without giving me a build error. Honestly I have no idea why it failed with the error message before, but is working now. The suggestion by @cartant in the comments is an explicit way to specify the result and that works as well.

推荐答案

您应该能够为catchError显式指定类型参数,以表明它不会返回发出值的可观察对象-即,它将返回Observable<never>-并将始终抛出:

You should be able to explicitly specify the type parameters to catchError to indicate that it won't return an observable that emits a value - that is, it will return Observable<never> - and will always throw:

getAlbums(): Observable<Album[]> {
    return this.httpClient.get<Album[]>(this.config.urls.url("albums"))
        .pipe(
            map(albumList => this.albumList = albumList),
            catchError<Album[], never>(new ErrorInfo().parseObservableResponseError)
        );           
}

这样做将看到从pipe调用推断出的类型为Observable<Album[]>.

Doing so will see the type inferred from the pipe call to be Observable<Album[]>.

这篇关于如何使用catchError()并在rxJs 6.0中仍返回类型化的Observable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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