[英]rxjs catchError operator not propagating the error after the Angular router navigate method is called
I have a decorator which adds a catchError operator to the observable returned by the decorated method.我有一个装饰器,它向装饰方法返回的 observable 添加一个 catchError 运算符。 When an error happens Angular router is used to navigate the back to a default route.当发生错误时,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; }; }
This works but the error does not propagate even though return throwError(error);
这有效但错误不会传播,即使return throwError(error);
is hit (tested).被击中(测试)。 If the navigate method call is commented out the error is correctly propagated.如果将导航方法调用注释掉,则会正确传播错误。 Any idea what can cause this?知道什么会导致这种情况吗?
i think you are losing the context here, can you change it to an arrow function and try again我认为您在这里丢失了上下文,您可以将其更改为箭头函数并重试吗
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.