繁体   English   中英

rxjs catchError 运算符在调用 Angular 路由器导航方法后不传播错误

[英]rxjs catchError operator not propagating the error after the Angular router navigate method is called

我有一个装饰器,它向装饰方法返回的 observable 添加一个 catchError 运算符。 当发生错误时,Angular 路由器用于导航回默认路由。

 export function redirectOnError(route: string) { return function (target: any, key: string, descriptor: PropertyDescriptor) { const method= descriptor.value; descriptor.value = function () { return method.apply(this, arguments).pipe( catchError((error: HttpErrorResponse) => { this.router.navigate([route]);//this exists on the decorated object return throwError(error); }) ); }; return descriptor; }; }

这有效但错误不会传播,即使return throwError(error); 被击中(测试)。 如果将导航方法调用注释掉,则会正确传播错误。 知道什么会导致这种情况吗?

我认为您在这里丢失了上下文,您可以将其更改为箭头函数并重试吗

 descriptor.value = () => {
  return method.apply(this, arguments).pipe(
    catchError((error: HttpErrorResponse) => {
      this.router.navigate([route]);//this exists on the decorated object
      return throwError(error);
    })
  );
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM