扩展 http class 用于自定义用法 ionic2/Angular2 导致错误
extending http class for custom usage ionic2/Angular2 causing error
我正在构建一个使用 jwt 令牌的应用程序。仍然有一些不需要它们的路由,但大多数调用都需要令牌。所以我想扩展 http class 并添加我的自定义 headers。我仍然想使用原始的 http class 进行正常调用。我在网上(和在 Whosebug 上)阅读了这方面的内容。但由于某些原因,我收到以下错误:
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,
但我尝试的所有操作都会导致相同的错误。我没有收到此错误及其发生的原因。
经过一些调试后,我发现我将 SecureHttpService 作为提供者添加了 2 次。所以错误来了,因为我第二次提供 "SecureHttpService" 依赖项等不存在......我的错误。所以只需删除它,它就会起作用
我正在构建一个使用 jwt 令牌的应用程序。仍然有一些不需要它们的路由,但大多数调用都需要令牌。所以我想扩展 http class 并添加我的自定义 headers。我仍然想使用原始的 http class 进行正常调用。我在网上(和在 Whosebug 上)阅读了这方面的内容。但由于某些原因,我收到以下错误:
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,
但我尝试的所有操作都会导致相同的错误。我没有收到此错误及其发生的原因。
经过一些调试后,我发现我将 SecureHttpService 作为提供者添加了 2 次。所以错误来了,因为我第二次提供 "SecureHttpService" 依赖项等不存在......我的错误。所以只需删除它,它就会起作用