1
我很困惑:我通常在其他服务中注入并使用我的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
你是否在其他地方注入了ihttpInterceptor并且它是否工作?尝试删除其注入并评论它使用,并检查它是否工作。也可以将logginservice导入到其他文件之后。 – Vega