2017-02-09 7 views
0

jwtトークンを使用するアプリケーションをビルドします。それらを必要としないルートはまだいくつかありますが、ほとんどの呼び出しではトークンが必要です。だから私はhttpクラスを拡張し、私のカスタムヘッダーを追加したいと思った。私はまだ通常の呼び出しのために元のhttpクラスを使用したい。私はこれについてオンラインで(そしてstackoverflowで)読む。しかし、私は次のエラーを取得するいくつかのreasongのために:カスタム使用のためにhttpクラスを拡張するionic2/Angular2エラーの原因

EXCEPTION: Error in :0:0 caused by: No provider for ConnectionBackend! 

私のアプリモジュールは次のようになります。

@NgModule({ 
    declarations: [ 
     MyApp, 
     DashboardPage, 
    ], 
    imports: [ 
     IonicModule.forRoot(MyApp) 
    ], 
    bootstrap: [IonicApp], 
    entryComponents: [ 
     MyApp, 
     DashboardPage, 
    ], 
    providers: [ 
     { 
      provide: [Http,SecureHttpService], 
      deps: [XHRBackend, RequestOptions], 
      useFactory: (backend: XHRBackend, defaultOptions: RequestOptions) => { 
       return new SecureHttpService(backend, defaultOptions); 
      }, 
      useClass: SecureHttpService 
     }, 
     { 
      provide: ErrorHandler, 
      useClass: IonicErrorHandler, 
     }, 
     SecureHttpService 
    ] 
}) 
export class AppModule {} 

サービスは、この

import { Injectable } from '@angular/core'; 
import { Http, ConnectionBackend, Headers, RequestOptions, Response, RequestOptionsArgs} from '@angular/http'; 

@Injectable() 
export class SecureHttpService extends Http { 

    constructor(backend: ConnectionBackend, defaultOptions: RequestOptions) { 
     super(backend, defaultOptions); 
    } 
} 

のようなルックスを拡張し、私はそれを使用したいです

constructor (private http: Http, private secureHttp: SecureHttpService) {} 

私はまた、(HTTPを使用しない)、このように提供使用しようとしました:

provide: SecureHttpService, 

を除くすべて私は同じエラーで結果を試してみてください。私はこのエラーが発生していないと、なぜそれが起こっている。

+0

が動作する '{ を提供する:[HTTP、SecureHttpService]、 DEPS:[XHRBackend、RequestOptions]、 useFactory:(バックエンド:XHRBackend、defaultOptions:RequestOptions)=> {新を返す SecureHttpService(バックエンド、defaultOptions); }、 useClass:SecureHttpService } 'わかりませんが、useFactoryメソッドとuseClassメソッドの両方を使用することはできますか? – Nikolai

答えて

0

私はSecureHttpServiceをプロバイダとして2回追加していました。私が "SecureHttpService"を提供する2度目の依存関係などは存在しないのでエラーが発生しました....私の間違い。そうそれを削除し、それが

関連する問題