catchError之后合并管道死

时间:2018-11-27 16:39:03

标签: angular rxjs material rxjs-pipeable-operators

我试图使用Angular材质分页器和排序,并从material.angular.io示例中获取一些代码。这部分:

ngOnInit() {
    this.exampleDatabase = new ExampleHttpDao(this.http);

    // If the user changes the sort order, reset back to the first page.
    this.sort.sortChange.subscribe(() => this.paginator.pageIndex = 0);

    merge(this.sort.sortChange, this.paginator.page)
      .pipe(
        startWith({}),
        switchMap(() => {
          this.isLoadingResults = true;
          return this.exampleDatabase!.getRepoIssues(
            this.sort.active, this.sort.direction, this.paginator.pageIndex);
        }),
        map(data => {
          // Flip flag to show that loading has finished.
          this.isLoadingResults = false;
          this.isRateLimitReached = false;
          this.resultsLength = data.total_count;

          return data.items;
        }),
        catchError(() => {
          this.isLoadingResults = false;
          // Catch if the GitHub API has reached its rate limit. Return empty data.
          this.isRateLimitReached = true;
          return observableOf([]);
        })
      ).subscribe(data => this.data = data);
}

当服务器返回错误并由catchError处理时,对Ang分页停止进行排序以将请求发送到服务器。他们的例子有什么问题?

2 个答案:

答案 0 :(得分:2)

这是可观察的工作方式,在onComplete或onError的情况下,可观察的对象将停止。如果要恢复它,则可以在catchError之后添加重复操作符,您的可观察源是热门的Observalbe,因此您不必担心自动无限循环。或者,您可以查看repeatWhen运算符

merge(this.sort.sortChange, this.paginator.page)
  .pipe(
    startWith({}),
    switchMap(() => {
      this.isLoadingResults = true;
      return this.exampleDatabase!.getRepoIssues(
        this.sort.active, this.sort.direction, 
        this.paginator.pageIndex).pipe(
          map(data => {
             // Flip flag to show that loading has finished.
             this.isLoadingResults = false;
             this.isRateLimitReached = false;
             this.resultsLength = data.total_count;
             return data.items;
         }),
          catchError(() => {
             this.isLoadingResults = false;
             this.isRateLimitReached = true;
             return observableOf([]);
          })
        );
    })
  ).subscribe(data => this.data = data)

答案 1 :(得分:0)

或者,您可以在switchMap内捕获错误,这不会杀死合并的一个。

location ~ \.php$ {
   include snippets/fastcgi-php.conf;
   ...
}