扩展http类以便自定义使用ionic2 / Angular2导致错误

时间:2021-09-22 23:03:44

Im building a app wich uses jwt tokens. there are still a few routes that dont need them but most of the calls need a token. So i wanted to extend the http class and add my custom headers. i still want to use the original http class for the normal calls. I read online (and on *) about this. But for some reasong i get the following error:

我正在构建一个使用jwt令牌的应用程序。仍然有一些路由不需要它们,但大多数呼叫需要一个令牌。所以我想扩展http类并添加我的自定义标头。我仍然想使用原始的http类进行正常调用。我在线阅读(关于*)。但是对于某些原因我得到以下错误:

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

my app module looks like this:

我的app模块看起来像这样:

    @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 {}

the service extend looks like this

服务扩展看起来像这样

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);
    }
}

and i want to use it in another service like this:

我想在其他服务中使用它:

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

I have also tried to use the provide like this (without http):

我也试过使用像这样的提供(没有http):

provide: SecureHttpService,

but everything i try results in the same error. Im not getting this error and why its happening.

但我尝试的一切都会导致同样的错误。我没有得到这个错误,为什么会发生这种错误。

1 个解决方案

#1


0  

after some debugging a saw that i was adding the SecureHttpService 2 times as a provider. so the error came because the second time i provide "SecureHttpService" the dependencies etc are not there.... my mistake. so just remove it, and it will work

经过一些调试后,我发现我正在将SecureHttpService作为提供者添加2次。所以错误来了,因为我第二次提供“SecureHttpService”依赖关系等不存在....我的错误。所以只需删除它,它就会起作用

#1


0  

after some debugging a saw that i was adding the SecureHttpService 2 times as a provider. so the error came because the second time i provide "SecureHttpService" the dependencies etc are not there.... my mistake. so just remove it, and it will work

经过一些调试后,我发现我正在将SecureHttpService作为提供者添加2次。所以错误来了,因为我第二次提供“SecureHttpService”依赖关系等不存在....我的错误。所以只需删除它,它就会起作用