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。 t
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クラスには、次のフレームワークから来ている:角4.3のHttpClientをして https://teradata.github.io/covalent/#/components/http
ihttpInterceptorを別の場所に注入しても機能しますか?その注入を取り除き、それが使用するコメントとそれが機能するかどうかをチェックしてください。また、他のものの後ろにlogginserviceのインポートを置く。 – Vega