Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular HttpInterceptor检测401响应_Angular_Typescript_Rxjs - Fatal编程技术网

Angular HttpInterceptor检测401响应

Angular HttpInterceptor检测401响应,angular,typescript,rxjs,Angular,Typescript,Rxjs,我有一个工作代码,在服务器发出401响应时调用logout()方法。由于我已将Angular从5.2升级到7.0.3,它不再工作。我认为这一定是由于修改了HttpInterceptor接口或者rxjs进行了很多突破性的更改。现在它看起来像这样,并在代码下面抛出错误 export class UnauthInterceptor implements HttpInterceptor { private session: SessionProvider; constructor(privat

我有一个工作代码,在服务器发出401响应时调用logout()方法。由于我已将Angular从5.2升级到7.0.3,它不再工作。我认为这一定是由于修改了HttpInterceptor接口或者rxjs进行了很多突破性的更改。现在它看起来像这样,并在代码下面抛出错误

export class UnauthInterceptor implements HttpInterceptor {

  private session: SessionProvider;
  constructor(private injector:Injector) {}

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
      this.session = this.injector.get(SessionProvider);
      return next.handle(request).pipe(catchError(err => {
            if (err.status === 401) {
                // auto logout if 401 response returned from api
                this.session.logout();
            }
        })
      );
  }}
导出类UnauthiInterceptor实现HttpInterceptor{
非公开会议:会议提供者;
构造函数(专用注入器:注入器){}
拦截(请求:HttpRequest,下一步:HttpHandler):可观察{
this.session=this.injector.get(SessionProvider);
返回next.handle(request).pipe(catchError(err=>{
如果(错误状态===401){
//如果api返回401响应,则自动注销
this.session.logout();
}
})
);
}}
tsc抛出以下错误:

src/app/interceptors/unauth.interceptor.ts:19:51-错误TS2345: 类型为“(err:any)=>void”的参数不可分配给参数 类型“(错误:任何,捕获:可观察>)=> 可观察的输入。
类型“void”不可分配给类型“ObservableInput”

19返回next.handle(request).pipe(catchError(err=>{


您好,您必须返回一个带有catchError的observable,如:

return throwError(error)

您好,您必须返回一个带有catchError的observable,如:

return throwError(error)

您必须从
catchError
返回可观测值,您必须从
catchError
返回可观测值,谢谢。顺便说一句,vscode仍然会抱怨一些奇怪的错误,但tsc运行时没有错误。谢谢。顺便说一句,vscode仍然会抱怨一些奇怪的错误,但tsc运行时没有错误。