未注入扩展 Http 中的自定义服务
Custom service in extended Http not getting injected
我想扩展 Http
提供程序以拦截所有传递 403 状态的请求以处理自动注销。
我的自定义 InterceptingHttp
应该声明为 Http
提供商,所以我不需要关心 "special" http 提供商。
我必须遵循:
我的自定义 Http 提供商
import { Injectable } from '@angular/core';
import { Request, XHRBackend, RequestOptions, Response, Http, RequestOptionsArgs } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { AuthenticationService } from './../services/authentication.service';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
@Injectable()
export class InterceptingHttpService extends Http {
constructor(backend: XHRBackend, defaultOptions: RequestOptions, private authenticationService: AuthenticationService) {
super(backend, defaultOptions);
}
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
var self = this;
return super.request(url, options).catch((res: Response) => {
if (res.status === 403) {
console.log('intercepted');
self.authenticationService.logout();
}
return Observable.throw(res);
});
}
}
在 NgModule 中作为常规 Http 提供者的声明
@NgModule({
imports: [
BrowserModule,
ToastyModule.forRoot(),
HttpModule,
FormsModule,
routing,
Ng2BootstrapModule,
PaginationModule
],
declarations: [
AppComponent,
NavHeaderComponent,
FooterCopyrightComponent,
InventoryRootComponent,
InventoryTreeComponent,
InventoryDetailComponent,
SetModelComponent,
MetadataListComponent,
MetadataDetailComponent,
ScriptGeneratorComponent,
SetDetailComponent,
SetVersionComponent,
SetContainerTreeComponent,
SetContainerDetailComponent,
FilterByPropertyPipe,
OrderContainersByLeafPipe,
ResolveStateId,
ConfirmComponent,
FocusDirective
],
providers: [
SetService,
SetTypeService,
SetContainerService,
StateService,
NotificationService,
MetadataService,
MetadataTypeService,
EntityService,
SetContainerMetadataService,
AuthenticationService,
InterceptingHttpService,
ConfirmService,
SiteVersionService,
{
provide: Http,
useFactory: (backend: XHRBackend, defaultOptions: RequestOptions, authenticationService: AuthenticationService) => {
return new InterceptingHttpService(backend, defaultOptions, authenticationService);
},
deps: [XHRBackend, RequestOptions, AuthenticationService]
},
],
bootstrap: [AppComponent]
})
它已加载并且确实拦截了所有 403 响应。唯一奇怪的是,authenticationService
未定义。
我想我的供应商声明可能有误。我试图将 AuthenticationService
添加到 deps
数组,这只会导致循环依赖错误。
我的错误在哪里?如何在我的扩展 Http 提供程序中使用我的 AuthenticationService
?
您需要将 AuthenticationService
添加到 deps
deps: [XHRBackend, RequestOptions, AuthenticationService]
当 Http
被注入到 AuthenticationService
时,这将导致 DI 无法解析的循环依赖。
请参阅 了解如何解决循环依赖的方法。
我想扩展 Http
提供程序以拦截所有传递 403 状态的请求以处理自动注销。
我的自定义 InterceptingHttp
应该声明为 Http
提供商,所以我不需要关心 "special" http 提供商。
我必须遵循:
我的自定义 Http 提供商
import { Injectable } from '@angular/core';
import { Request, XHRBackend, RequestOptions, Response, Http, RequestOptionsArgs } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { AuthenticationService } from './../services/authentication.service';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
@Injectable()
export class InterceptingHttpService extends Http {
constructor(backend: XHRBackend, defaultOptions: RequestOptions, private authenticationService: AuthenticationService) {
super(backend, defaultOptions);
}
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
var self = this;
return super.request(url, options).catch((res: Response) => {
if (res.status === 403) {
console.log('intercepted');
self.authenticationService.logout();
}
return Observable.throw(res);
});
}
}
在 NgModule 中作为常规 Http 提供者的声明
@NgModule({
imports: [
BrowserModule,
ToastyModule.forRoot(),
HttpModule,
FormsModule,
routing,
Ng2BootstrapModule,
PaginationModule
],
declarations: [
AppComponent,
NavHeaderComponent,
FooterCopyrightComponent,
InventoryRootComponent,
InventoryTreeComponent,
InventoryDetailComponent,
SetModelComponent,
MetadataListComponent,
MetadataDetailComponent,
ScriptGeneratorComponent,
SetDetailComponent,
SetVersionComponent,
SetContainerTreeComponent,
SetContainerDetailComponent,
FilterByPropertyPipe,
OrderContainersByLeafPipe,
ResolveStateId,
ConfirmComponent,
FocusDirective
],
providers: [
SetService,
SetTypeService,
SetContainerService,
StateService,
NotificationService,
MetadataService,
MetadataTypeService,
EntityService,
SetContainerMetadataService,
AuthenticationService,
InterceptingHttpService,
ConfirmService,
SiteVersionService,
{
provide: Http,
useFactory: (backend: XHRBackend, defaultOptions: RequestOptions, authenticationService: AuthenticationService) => {
return new InterceptingHttpService(backend, defaultOptions, authenticationService);
},
deps: [XHRBackend, RequestOptions, AuthenticationService]
},
],
bootstrap: [AppComponent]
})
它已加载并且确实拦截了所有 403 响应。唯一奇怪的是,authenticationService
未定义。
我想我的供应商声明可能有误。我试图将 AuthenticationService
添加到 deps
数组,这只会导致循环依赖错误。
我的错误在哪里?如何在我的扩展 Http 提供程序中使用我的 AuthenticationService
?
您需要将 AuthenticationService
添加到 deps
deps: [XHRBackend, RequestOptions, AuthenticationService]
当 Http
被注入到 AuthenticationService
时,这将导致 DI 无法解析的循环依赖。
请参阅