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

删除回忆录丶 提交于 2019-12-03 14:40:48

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/39847356/angular2-cant-resolve-all-parameters-error-while-injecting-http-into-a-cust

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!