如何将我的服务注入ExceptionHandler [英] How to Inject my service to ExceptionHandler

查看:120
本文介绍了如何将我的服务注入ExceptionHandler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在其他由角度2自动注入的地方使用了我的服务. 我想在ExceptionHandler中使用相同的服务. 但是服务不会将数据发布到服务器. 我通过了调试器,然后调用了我的服务.

I used my service in other places injected automaticaly by angular 2. I want to use the same service in ExceptionHandler. But service doesn't post data to server. I went through debuger and my service invokes.

class MyExceptionHandler extends ExceptionHandler {
  rbJSLogger: RBLoggerService;

  constructor() {
    super(null,null);
    var injector = ReflectiveInjector.resolveAndCreate([
      RBLoggerService,
      JSONP_PROVIDERS,
      Http,
      ConnectionBackend,
      HTTP_PROVIDERS
    ]);
    this.rbJSLogger = injector.get(RBLoggerService);
  }
  call(error, stackTrace = null, reason = null){
    // console.error(stackTrace);
    this.rbJSLogger.searchBy("asd");
  }
}

推荐答案

更新 ExceptionHandler重命名为ErrorHandler https://stackoverflow.com/a/35239028/217408

原始

此代码

var injector = ReflectiveInjector.resolveAndCreate([...]);

创建一个新的独立注入器,该注入器对Angular应用程序中提供的服务一无所知.

creates a new independent injector that doesn't know anything about services provided in your Angular applications.

您可能希望在应用程序中注入Angular使用的注入器,例如

You might want to inject the injector used by Angular in your application like

class MyExceptionHandler extends ExceptionHandler {
  rbJSLogger: RBLoggerService;

  constructor(injector:Injector) {
    super(null,null);
    this.rbJSLogger = injector.get(RBLoggerService);
  }
  call(error, stackTrace = null, reason = null){
    // console.error(stackTrace);
    this.rbJSLogger.searchBy("asd");
  }
}

或者只是

class MyExceptionHandler extends ExceptionHandler {
  rbJSLogger: RBLoggerService;

  constructor(private rbJSLogger:RBLoggerService) {
    super(null,null);
  }
  call(error, stackTrace = null, reason = null){
    // console.error(stackTrace);
    this.rbJSLogger.searchBy("asd");
  }
}

这篇关于如何将我的服务注入ExceptionHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆