Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 675
Picci Avatar asked Oct 04 '16 08:10

Picci


1 Answers

@Injectable() // <<<=== required if the constructor has parameters  export class ErrorHandlerLogger extends ErrorHandler { 
like image 192
Günter Zöchbauer Avatar answered Sep 25 '22 19:09

Günter Zöchbauer