【发布时间】:2016-08-03 04:27:20
【问题描述】:
我有一个像这样的自定义异常处理程序,我试图在其中注入服务 (ErrorReportingService)
import { ExceptionHandler, Injectable } from '@angular/core';
import {ErrorReportingService } from '../services/ErrorReportingService';
@Injectable()
export class InsightsExceptionHandler extends ExceptionHandler {
constructor(private _errorReporter: ErrorReportingService) {
super(null, null);
}
call(error, stackTrace = null, reason = null) {
this._errorReporter.logError(error);
}
}
我尝试注入的服务如下所示
import { Http, Response } from '@angular/http';
import { Injectable, Inject } from '@angular/core';
@Injectable()
export class ErrorReportingService {
private ErrorReportingUrl = `/property/insights/api/errorreporting`;
constructor(private _http: Http) { }
logError(error: any) {
var body = JSON.stringify(error);
this._http.post(this.ErrorReportingUrl, body, null);
}
}
ErrorReportingService 注册在应用组件的 providers 下。
我也尝试过像这样以不同的方式注入服务:
export class InsightsExceptionHandler extends ExceptionHandler {
private _errorReporter: ErrorReportingService;
constructor( @Inject(ErrorReportingService) errorReporter: ErrorReportingService) {
super(null, null);
this._errorReporter = errorReporter;
}
......
我在尝试运行应用程序时收到此错误:
Error: (SystemJS) EXCEPTION: Error during instantiation of ApplicationRef_! (ApplicationRef -> ApplicationRef_).
ORIGINAL EXCEPTION: No provider for ErrorReportingService! (ExceptionHandler -> ErrorReportingService)
ORIGINAL STACKTRACE:
Error: DI Exception
【问题讨论】:
标签: angular dependency-injection angular2-services angular2-injection