为了避免循环依赖,我使用Injector
注入AuthService
但是当我运行应用程序 Angularintercept()
在设置属性之前执行该方法authService
!!!
@Injectable()
export class TokenInterceptorService implements HttpInterceptor{
private authService;
constructor(private injector: Injector) {
setTimeout(() => {
this.authService = injector.get(AuthService);
console.log('===========================================',this.authService);
});
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
request = request.clone({
setHeaders: {
Authorization: `Bearer ${this.authService.getToken()}`
}
});
return next.handle(request);
}
}