Http
プロバイダを拡張して、自動ログアウトを処理する403ステータスを提供するすべてのリクエストを傍受したいと考えています。 私のカスタムInterceptingHttp
はHttp
プロバイダーと宣言されるべきですので、私は "特別な" httpプロバイダーを気にする必要はありません。拡張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
は未定義です。
私はおそらく私のプロバイダの宣言で間違いをしていると思います。私はdeps
配列にAuthenticationService
を追加しようとしましたが、これは循環依存性エラーに終わっただけでした。
私の間違いはどこですか?拡張HTTPプロバイダでAuthenticationService
を使用するにはどうすればよいですか?
'()=>'を使用する場合、 'self'は必要ありません。 –
コンストラクタを 'constructor(...){super(...);}のようにチェックインしましたか? console.log(authenticationService); } '値が渡されたかどうか? –
@GünterZöchbauerそれは正しいです。これは残っていた。ありがとう –