Get "Unable to resolve all parameters" when I provide Interceptor

I am trying to implement an HttpInterceptor. When I add it to @NgModule, I get the following error in Chrome:

Uncaught Error: Can't resolve all parameters for JwtInterceptor: (?, ?). at syntaxError (compiler.js:466) at CompileMetadataResolver._getDependenciesMetadata (compiler.js:15547) at CompileMetadataResolver._getTypeMetadata (compiler.js:15382) 

Spent a lot of time on search engines, I don’t know what to do ...

This is how I provide Interceptor in the AppModule:

 ... providers: [ { provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true } ], ... 

Here is the interceptor itself, nothing fantastic:

 export class JwtInterceptor implements HttpInterceptor { constructor(private inj: Injector, private logger: Logger) { this.logger.l(true)('Interceptor >>'); } intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { this.logger.l(true)('interceptor >>'); const auth = this.inj.get(AuthService); // add token to the request: const authReq = req.clone({ setHeaders: { Authorization: `Bearer ${auth.getToken()}` } }); // return next.handle(authReq); return next.handle(authReq).do((event: HttpEvent<any>) => { if (event instanceof HttpResponse) { // todo: get refreshed token if it exists: } }, (err: any) => { if (err instanceof HttpErrorResponse) { if (err.status === 401) { auth.collectFailedRequest(authReq); // todo: redirect to the login route or show a modal } } }); } } 
+5
source share
1 answer

My bad, I forgot to add @Injectable ()

+9
source

Source: https://habr.com/ru/post/1273892/


All Articles