简体   繁体   中英

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

@Injectable() // <<<=== required if the constructor has parameters 
export class ErrorHandlerLogger extends ErrorHandler {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM