Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

angular - How to Inject my service to 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");
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

update ExceptionHandler was renamed to ErrorHandler https://stackoverflow.com/a/35239028/217408

orgiginal

This code

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

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

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");
  }
}

or just

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");
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...