Answer the question
In order to leave comments, you need to log in
Why is the interceptor not working?
I am using angular 5.1.3. I'm trying to write an interceptor that adds a header to any http request 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);
}
}
...
...
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 { }
compiler.js:19541 Uncaught Error: Provider parse errors:
Cannot instantiate cyclic dependency! InjectionToken_HTTP_INTERCEPTORS ("[ERROR ->]"): in NgModule AppModule in ./[email protected]:-1
at NgModuleProviderAnalyzer.parse (compiler.js:19541)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question