简体   繁体   中英

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. When an error happens Angular router is used to navigate the back to a default route.

 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); 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);
    })
  );
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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