我正在将应用程序从Angular 5升级到Angular8。我创建了HttpInterceptor,但出现此错误:
错误next.handle不是函数(core.js)。
我检查了HttpClientModule仅加载一次,所以我不确定是什么导致了此问题。
您能帮我解决这个问题吗?
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Observable, throwError as observableThrowError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { AppService } from './services/app.service';
@Injectable(
{
providedIn: 'root'
})
export class AuthHttpInterceptor implements HttpInterceptor {
constructor(private authService: AppService, private router: Router) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
//const jwt = this.authService.getAuthToken();
const jwt = localStorage.getItem('jwt');
const authRequest = req.clone({
setHeaders: { authorization: `Bearer ${jwt}` }, withCredentials: true
});
return next.handle(req).pipe(
catchError((err, caught) => {
if (err.status === 401) {
this.router.navigate(['/login'], {
queryParams: { redirectUrl: this.router.routerState.snapshot.url }
});
}
return new Observable<HttpEvent<any>>();
})
);
}
}
答案 0 :(得分:0)
发现了问题,在app.module上注入了HttpBackend,我将其删除并开始工作。 这可能对其他人有帮助,因为我们在此上花费了将近半天。