2017-02-09 106 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”的依赖关系等不存在....我的错误。所以只是将其删除,和将工作