2016-12-19 6 views
1

Httpプロバイダを拡張して、自動ログアウトを処理する403ステータスを提供するすべてのリクエストを傍受したいと考えています。 私のカスタムInterceptingHttpHttpプロバイダーと宣言されるべきですので、私は "特別な" 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を使用するにはどうすればよいですか?

+1

'()=>'を使用する場合、 'self'は必要ありません。 –

+0

コンストラクタを 'constructor(...){super(...);}のようにチェックインしましたか? console.log(authenticationService); } '値が渡されたかどうか? –

+0

@GünterZöchbauerそれは正しいです。これは残っていた。ありがとう –

答えて

1

あなたはHttpAuthenticationServiceに注入されると、これはDIが解決できない循環依存関係が発生しますAuthenticationService

deps: [XHRBackend, RequestOptions, AuthenticationService] 

depsに追加する必要があります。

循環依存を回避する方法については、DI with cyclic dependency with custom HTTP and ConfigServiceを参照してください。

+0

私はこれを試してきましたが、角度の新しいバージョンでも '循環依存関係をインスタンス化できません!Http:./AppModuleのNgModule AppModuleで' –

+0

完全なngModule宣言 –

+0

'AuthenticationService'はおそらく' Http'を注入します。おそらく多分、http://stackoverflow.com/questions/40860202/di-with-cyclic-dependency-with-custom-http-and-configservice/40860233#40860233 –

関連する問題