在我的错误处理程序中,当遇到错误时,我尝试导航到另一个页面。
我尝试获取项目列表,如果失败我想导航到“./error401”。我一直在尝试这样做:
用户表组件:
export class UserDataSource extends DataSource<any>{
constructor(private authservice: AuthService) {
super();
}
connect(): any {
return this.authservice.GetInstallation();
}
disconnect() { }
}
这个调用可能有点奇怪。它的作用是根据 token 调用其他人查看我拥有的 ID。然后在下一次调用中使用该 ID
auth.service
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”但得到
errror ERROR TypeError: Cannot read property 'navigate' of undefined
这是完整的错误日志:
ERROR TypeError: Cannot read property 'navigate' of undefined at CatchSubscriber.push../src/app/myerrorHandle.ts.myerrorHandle.handleError [as selector] (myerrorHandle.ts:18) at CatchSubscriber.push../node_modules/rxjs/_esm5/internal/operators/catchError.js.CatchSubscriber.error (catchError.js:33) at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._error (Subscriber.js:80) at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.error (Subscriber.js:60) at MapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._error (Subscriber.js:80) at MapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.error (Subscriber.js:60) at FilterSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._error (Subscriber.js:80) at FilterSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.error (Subscriber.js:60) at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/OuterSubscriber.js.OuterSubscriber.notifyError (OuterSubscriber.js:13) at InnerSubscriber.push../node_modules/rxjs/_esm5/internal/InnerSubscriber.js.InnerSubscriber._error (InnerSubscriber.js:18)
最佳答案
正如@user184994所说:
Change catchError to catchError((err) => this.myerrorhandle.handleError(err))) otherwise (without arrow functions) the context of this is lost
关于node.js - Angular 6 错误类型错误 : Cannot read property 'navigate' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52112949/