Angular2 - 'Can't resolve all parameters' error while injecting http into a custom service

Picci picture Picci · Oct 4, 2016 · Viewed 16.3k times · Source

I have built an ErrorHandlerLogger which is a service which extends ErrorHandler and logs error messages into a remote repository.

ErrorHandlerLogger requires the Angular http client provided by the HttpModule.

In the ErrorHandlerModule I import HttpModule and define ErrorHandlerLogger as provider.

In the AppModule I import ErrorHandlerModule.

When I launch the app I get the following error message

Uncaught Error: Can't resolve all parameters for ErrorHandlerLogger: (?).

Here my code

ErrorHandlerModule

import { NgModule, ErrorHandler } from '@angular/core';
import { HttpModule } from '@angular/http';

import {ErrorHandlerLogger} from './error-handler-logger';

@NgModule({
    declarations: [],
    exports: [],
    imports: [
        HttpModule
    ],
    providers: [
        {provide: ErrorHandler, useClass: ErrorHandlerLogger}
    ]
})
export class ErrorHandlerModule {}

ErrorHandlerLogger

import { ErrorHandler } from '@angular/core';
import { Http, Headers, RequestOptions, Response } from '@angular/http';
import { Observable }     from 'rxjs/Observable';
import './rxjs-operators';

export class ErrorHandlerLogger extends ErrorHandler {
    constructor(private http: Http) {
        super();
     }

    handleError(error) {
        // my logic
    }

}

AppModule

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import {ErrorHandlerModule} from './error-manager/error-handler.module';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    routing,
    ErrorHandlerModule
  ],
  providers: [appRoutingProviders],
  bootstrap: [AppComponent]
})
export class AppModule { }

Any help is very much appreciated

Answer

Günter Zöchbauer picture Günter Zöchbauer · Oct 4, 2016
@Injectable() // <<<=== required if the constructor has parameters 
export class ErrorHandlerLogger extends ErrorHandler {