Reputation: 4284
I am puzzled: I usually inject and use my LoggingService in other services and it works. This is my very simple LoggingService:
import { Injectable } from '@angular/core';
@Injectable()
export class LoggingService {
//[...]
logInfo(obj: any) {
if (this.logging) console.info('[INFO] ' + obj);
}
}
In the following class, my injected service is undefined:
interceptor.service.ts
import { LoggingService } from './../logging/logging.service';
import { RequestOptionsArgs, Response } from '@angular/http';
import { Injectable, Injector, OnInit } from '@angular/core';
import { IHttpInterceptor } from '@covalent/http';
@Injectable()
export class InterceptorService implements IHttpInterceptor {
constructor(private logger: LoggingService) {
console.log('logger = ' + logger); // UNDEFINED
}
onRequest(requestOptions: RequestOptionsArgs): RequestOptionsArgs {
this.logger.logDebug(requestOptions); // Obviously does not work
return requestOptions;
}
// [...] Other methods
}
app.module.ts
import { InterceptorService } from './services/interceptor/interceptor.service';
import { CovalentHttpModule, IHttpInterceptor } from '@covalent/http';
import { LoggingService } from './services/logging/logging.service';
@NgModule({
imports: [
// [...]
CovalentHttpModule.forRoot({
interceptors: [{
interceptor: InterceptorService, paths: ['**'],
}],
}),
],
providers: [
LoggingService,
InterceptorService,
// [...]
],
bootstrap: [AppComponent]
})
export class AppModule { }
How is this class different from the other ones (where the injection works ?
I also tried injecting the service using injector:
loggingService : LoggingService;
constructor(private injector: Injector) { }
// [...]
loggingService = this.injector.get(LoggingService);
But it is always undefined. What am I missing ?
The IHttpInterceptor class comes from the following framework: https://teradata.github.io/covalent/#/components/http
Upvotes: 1
Views: 559
Reputation: 4284
With Angular 4.3's Httpclient, I eventually gave up and wrote my own interceptor. The problem I had was probably related to Covalent's Interceptor service and configuration.
Upvotes: 0