我很困惑:我经常在其他服务中注入并使用我的LoggingService,它可以正常工作。这是我非常简单的LoggingService:
import { Injectable } from '@angular/core';
@Injectable()
export class LoggingService {
//[...]
logInfo(obj: any) {
if (this.logging) console.info('[INFO] ' + obj);
}
}
在下面的课程中,我的注入服务未定义:
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 { }
这个班级与其他班级有什么不同(注射有效?
我也尝试使用进样器注入服务:
loggingService : LoggingService;
constructor(private injector: Injector) { }
// [...]
loggingService = this.injector.get(LoggingService);
但它始终未定义。我缺少什么?
IHttpInterceptor类来自以下框架: https://teradata.github.io/covalent/#/components/http
答案 0 :(得分:0)
使用Angular 4.3的Httpclient,我最终放弃并编写了自己的拦截器。我遇到的问题可能与Covalent的Interceptor服务和配置有关。