2017-02-09 111 views
0

我正在構建一個使用jwt令牌的應用程序。仍然有幾條路線不需要它們,但大多數呼叫需要令牌。所以我想擴展http類並添加我的自定義標題。我仍然想使用原始的http類進行正常的調用。我在線閱讀(和在stackoverflow)關於這一點。但對於一些reasong我得到以下錯誤:爲自定義用法擴展http class ion2/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」的依賴關係等不存在....我的錯誤。所以只是將其刪除,和將工作