本文介绍了Angular2 - 将 http 注入自定义服务时出现“无法解决所有参数"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个 ErrorHandlerLogger,它是一个扩展 ErrorHandler 并将错误消息记录到远程存储库的服务.

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

ErrorHandlerLogger 需要 HttpModule 提供的 Angular http 客户端.

ErrorHandlerLogger requires the Angular http client provided by the HttpModule.

ErrorHandlerModule 中,我导入了 HttpModule 并将 ErrorHandlerLogger 定义为提供者.

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

AppModule 中,我导入了 ErrorHandlerModule.

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: (?).

这是我的代码

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
    }

}

应用模块

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 { }

非常感谢任何帮助

推荐答案

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

这篇关于Angular2 - 将 http 注入自定义服务时出现“无法解决所有参数"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 16:09