0

我想为按钮添加加载状态,但我不想为它提供任何服务。我对管道感到非常困惑,而且我不了解正确的语法。当 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
  },
  )
);

}

4

1 回答 1

0

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

在 observable 关闭后,您可以使用终结器设置所需的状态。

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

例如

  this.service.context.pipe(
    filter((value) => !!value)),
    tap(v=>this.isLoading=false)).subscribe(...);
于 2021-10-25T07:10:35.087 回答