【问题标题】:Rxjs finalize after code in subscribtion ends订阅中的代码结束后 Rxjs 完成
【发布时间】:2020-07-13 15:50:31
【问题描述】:

我有一个 Angular 应用程序 (9) 并且有多个组件请求计算操作而不是休息,并在不同的编辑器中显示结果。 现在,在我发出请求之前,我调用了一个加载面板,在请求完成后,我在 finalize 回调中隐藏了加载面板。像这样的:

this.isloading = false;
httpclient.get('http://localhost/calculate').pipe(finalize(() =>this.isloading=false).subscribe(v=>{
 //make some operations with the data and set editor values
});

现在加载面板在设置所有编辑器值之前消失。如果请求完成直接调用finalize回调,但是如果subscribe或者error里面的代码完成了怎么调用finalize之类的呢?

【问题讨论】:

    标签: angular observable rxjs6 rxjs-pipeable-operators rxjs-observables


    【解决方案1】:

    subscribe 块的末尾调用它?看起来您的 finalize 正在您需要它的每个“地方”中使用(它不是在服务中抽象出来的)所以您不妨直接调用 this.isLoading = false; 作为您的 subscribeerror 的最后一条语句块

    【讨论】:

    • 是的,这是我可以这样做的最后一个选项,在订阅结束和错误回调的 and 时调用它。但是所以我每次都得调用它两次
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 2021-11-16
    • 2020-09-28
    • 1970-01-01
    • 2021-12-27
    相关资源
    最近更新 更多