【问题标题】:How to set a asynchronously a variable in an angular pipe?如何在角管道中异步设置变量?
【发布时间】:2021-12-10 16:48:50
【问题描述】:

我想为按钮添加加载状态,但我不想为它提供任何服务。 我对管道感到非常困惑,而且我不了解正确的语法。当 this.apiService.exportReport 完成时,我可以将 isLoading 设置为 false 吗?

  public exportReportResults(format: ExportFormat) {
this.isLoading = true;
this.sink.add(
  this.service.context.pipe(
    filter((value) => !!value))
    .subscribe(
      (data) => {
    const items: any[] = [];
    data.context.groups.forEach(function (group) {
      items.push(group.items.map(({ id }) => id));
    });
    const itemQuery: string = "'{" + items.toString() + "}'";
    this.apiService.exportReport(format, itemQuery);
    this.isLoading = false; //not good this way of course
  },
  )
);

}

【问题讨论】:

    标签: angular async-await rxjs


    【解决方案1】:

    订阅接受 3 个参数:值消费者、异常处理程序、终结器。

    您可以在 observable 关闭后使用 finalizer 设置所需的状态。

    如果您的 observable 根本没有关闭,您必须在消费者中(就像您所做的那样)或在管道中的某处使用 tap 操作符

    例如

      this.service.context.pipe(
        filter((value) => !!value)),
        tap(v=>this.isLoading=false)).subscribe(...);
    

    【讨论】:

    • 我无法让它工作,终结器不适合我的情况,因为我只在销毁时关闭它。我试过水龙头,我真的不明白如何在这里使用它。
    猜你喜欢
    • 1970-01-01
    • 2020-02-15
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 2016-06-20
    • 1970-01-01
    • 2023-03-29
    • 2018-05-03
    相关资源
    最近更新 更多