Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DOES NOT WORK
- @Injectable()
- export class AuthInterceptor implements HttpInterceptor {
- constructor(
- private router: Router, private auth: Auth
- ) {}
- intercept( req: HttpRequest<any>, next: any ): Observable<HttpEvent<any>> {
- const copiedReq = req.clone({
- headers: req.headers.set(
- 'authorization', 'Bearer ' + this.auth.getToken()
- )
- });
- return next.handle(copiedReq);
- }
- }
- // RUNTIME ERROR
- compiler.js:19387 Uncaught Error: Provider parse errors:
- Cannot instantiate cyclic dependency! InjectionToken_HTTP_INTERCEPTORS ("[ERROR ->]"): in NgModule AppModule in ./AppModule@-1:-1
- --------
- WORKING EXAMPLE
- @Injectable()
- export class AuthInterceptor implements HttpInterceptor {
- constructor( private injector: Injector, private router: Router) {}
- intercept( req: HttpRequest<any>, next: any ): Observable<HttpEvent<any>> {
- const auth = this.injector.get(Auth);
- const copiedReq = req.clone({
- headers: req.headers.set(
- 'authorization', 'Bearer ' + auth.getToken()
- )
- });
- return next.handle(copiedReq);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement