0
votes

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 stackoverflow) about this. But for some reasong i get the following error:

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

my app module looks like this:

    @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):

provide: SecureHttpService,

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

1
{ provide: [Http,SecureHttpService], deps: [XHRBackend, RequestOptions], useFactory: (backend: XHRBackend, defaultOptions: RequestOptions) => { return new SecureHttpService(backend, defaultOptions); }, useClass: SecureHttpService } I am not sure, but is it ok to use both useFactory and useClass methods? - Nikolai

1 Answers

0
votes

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