在我的错误处理程序中,当我遇到错误时,我试图导航到另一个页面。

我尝试获取项目列表,如果失败的话,我想导航到“ ./error401”。我一直在尝试这样做:

UserTable组件:

export class UserDataSource extends DataSource<any>{


  constructor(private authservice: AuthService) {
    super();
  }
  connect(): any {
    return this.authservice.GetInstallation();
  }

  disconnect() { }
}


这个电话可能有点奇怪。它的作用是打电话给其他人,以查看我根据令牌拥有的ID。然后在下一个通话中使用该ID

认证服务

  GetServiceProviderId(): Observable<Info> {
    return this.http.get<Info>(this.rooturl + 'info', { headers: this.reqHeader });
  }

  GetInstallation(): Observable<Installation[]> {
    return this.GetServiceProviderId().pipe(
      flatMap(info => {
        return this.http.get<Installation[]>
          (this.rooturl +
          "installation/?serviceproviderid=" +
          info.ServiceProviderId,
          { headers: this.reqHeader })
      }
    ), catchError(this.myerrorhandle.handleError))
  }
}


myerrorHandle

@Injectable({
  providedIn: 'root'
})
export class myerrorHandle {

  constructor(public router: Router) { }

  handleError(errorResponse: HttpErrorResponse) {
    switch (errorResponse.status) {
      case 401: {
        this.router.navigate(["./error401"])
        break;
      }
      default: {
        console.log("todo");
        break;
      }
    }
    return throwError('an error was thrown');
  }
}


这是我要导航到“ ./error401”但得到的


  错误错误TypeError:无法读取未定义的属性“ navigate”


这是完整的错误日志:


  错误TypeError:无法读取未定义的属性“导航”
      在CatchSubscriber.push ../ src / app / myerrorHandle.ts.myerrorHandle.handleError
  [作为选择器](myerrorHandle.ts:18)
      在CatchSubscriber.push ../ node_modules / rxjs / _esm5 / internal / operators / catchError.js.CatchSubscriber.error中
  (catchError.js:33)
      在MergeMapSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber._error中
  (Subscriber.js:80)
      在MergeMapSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber.error中
  (Subscriber.js:60)
      在MapSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber._error中
  (Subscriber.js:80)
      在MapSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber.error中
  (Subscriber.js:60)
      在FilterSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber._error中
  (Subscriber.js:80)
      在FilterSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber.error中
  (Subscriber.js:60)
      在MergeMapSubscriber.push ../ node_modules / rxjs / _esm5 / internal / OuterSubscriber.js.OuterSubscriber.notifyError
  (OuterSubscriber.js:13)
      在InnerSubscriber.push ../ node_modules / rxjs / _esm5 / internal / InnerSubscriber.js.InnerSubscriber._error(InnerSubscriber.js:18)

最佳答案

如@ user184994所说:


  将catchError更改为catchError((err)=>
  this.myerrorhandle.handleError(err)))否则(无箭头)
  函数)的上下文丢失

关于node.js - Angular 6错误TypeError:无法读取未定义的属性“navigate”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52112949/

10-16 11:48
查看更多