Angular:如何使用 RXJS 6 调用 finally() [英] Angular : how to call finally() with RXJS 6

查看:33
本文介绍了Angular:如何使用 RXJS 6 调用 finally()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 RXJS 5,现在当我将它升级到 6 时,我遇到了一些问题.

I was using RXJS 5, now as I upgraded it to 6, I am facing some problems.

以前我可以使用 catch 和 finally 但随着更新 catch 被 catchError 替换(在管道中)现在如何使用 finally?

Previously I was able to use catch and finally but as per update catch is replaced with catchError (with in the pipe) now how to use finally?

我还有一些问题:

我需要改变throw->throwError吗(在下面的代码Observable.throw(err);)

Do I need to change throw->throwError (in below code Observable.throw(err);)

import { Observable, Subject, EMPTY, throwError } from "rxjs";
import { catchError } from 'rxjs/operators';

return next.handle(clonedreq).pipe(
          catchError((err: HttpErrorResponse) => {
        if ((err.status == 400) || (err.status == 401)) {
            this.interceptorRedirectService.getInterceptedSource().next(err.status);
            return Observable.empty();
        } else {
            return Observable.throw(err);
        }
       }) 
        //, finally(() => {
        //  this.globalEventsManager.showLoader.emit(false);
        //});
      );

还有现在如何使用publish().refCount()?

Also how to use publish().refCount() now ?

推荐答案

  • 使用 throwError 而不是 Observable.throw,参见 https://github.com/ReactiveX/rxjs/blob/master/docs_app/content/guide/v6/migration.md#可观察类

    finally 已重命名为 finalize,您将在 pipe() 和其他运算符中使用它.

    finally was renamed to finalize and you'll use it inside pipe() among other operators.

    publish()refCount() 相同.两者都是您将在 pipe() 中使用的运算符.

    the same with publish() and refCount(). Both are operators you'll use inside pipe().

    这篇关于Angular:如何使用 RXJS 6 调用 finally()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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