Why doesn't the interceptor?

Use angular 5.1.3. Trying to write an interceptor that any http request, adds a header
Authorization: '12345'

My interceptor:
import { Injectable } from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor
} from '@angular/common/http';
import { AuthService } from './auth.service';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class TokenInterceptor implements HttpInterceptor {

 constructor(public auth: AuthService) {}

 intercept(request: HttpRequest<any>, next: HttpHandler): Observable<httpevent<any>> { 
 request = request.clone({
 setHeaders: {
 Authorization: '12345'
}
});
 return next.handle(request);
}

}</httpevent<any></any>


Plug it in app.module.ts so:
...
...
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { TokenInterceptor } from './Modules/Core/Services/auth.interceptor';

@NgModule({
....
...
 providers: [
{
 provide: HTTP_INTERCEPTORS,
 useClass: TokenInterceptor,
 multi: true
}
],
 bootstrap: [AppComponent]
})
export class AppModule { }


The result before sending the request in the browser console get this error message:
compiler.js:19541 Uncaught Error: Provider parse errors:
Cannot instantiate cyclic dependency! InjectionToken_HTTP_INTERCEPTORS ("[ERROR ->]"): in NgModule AppModule in ./AppModule@-1:-1
at NgModuleProviderAnalyzer.parse (compiler.js:19541)


Help please still send these headers
June 8th 19 at 16:36
1 answer
June 8th 19 at 16:38
Solution
constructor(public auth: AuthService) {}

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


and where you have connected the AuthService?
it's not just the property Declaration in the constructor. You activate Dependency Injection - dependency injection. Angular trying to pass to the constructor the instance AuthService, but can't because you never declared. Read https://angular.io/guide/dependency-injection

If your designer had the appearance of
constructor(public auth: any) {}
no mistakes would have happened - Edwin.Abbo commented on June 8th 19 at 16:41

Find more questions by tags HTTP headersAngular