簡體   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