Angular:防止api错误不显示HTML模板

问题描述 投票:0回答:1

[当我执行HTTP请求(例如GET请求)并且获得了针对无效给定数据的状态代码400时,它阻止了我的HTML模板显示在我的角度应用程序视图中。这就是我目前的处理方式:

this.myService.getData(neededData)
  .subscribe(
    (result) => {
      this.data = result;
    },
    (error) => { 
      if (error) { 
        console.log("error"); 
        this.anErrorHasOccured = true; 
      }
    }
  );

“错误”已在控制台中很好地显示,但是此错误使我的模板无法显示,如何解决?

angular api templates handle
1个回答
2
投票

您可以尝试这个吗:

this.myService.getData(neededData)
  .pipe(catchError(err => {
    console.err(err);
    return of([]);
  }))
  .subscribe(
    (result) => this.data = result,
  );
© www.soinside.com 2019 - 2024. All rights reserved.