I'm using Angular 2 RC2. I need to inject the Angular 2 Router into my custom ExceptionHandler class. However I get the following error
Error: Error: Cannot resolve all parameters for 'ErrorHandler'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'ErrorHandler' is decorated with Injectable.
I did try decorating private router: Router with @Inject to no avail. I'm using typescript, hence I don't think I need the @Inject attribute here.
My custom ExceptionHandler looks like this
import { ExceptionHandler } from '@angular/core';
import { Router } from '@angular/router';
export class ErrorHandler extends ExceptionHandler{
constructor(
private router: Router
){
super(null, null);
}
call(error, stackTrace = null, reason = null) {
console.log(error);
this.router.navigate(['/error']);
}
}
My main.ts looks like this
import { bootstrap } from '@angular/platform-browser-dynamic';
import { AppComponent } from './app.component';
import { provide, ExceptionHandler } from '@angular/core';
import { ErrorHandler } from './error-handler/error-handler';
import { HTTP_PROVIDERS } from '@angular/http';
import { ROUTER_PROVIDERS } from '@angular/router';
bootstrap(AppComponent, [
HTTP_PROVIDERS,
ROUTER_PROVIDERS,
provide(ExceptionHandler, {useClass: ErrorHandler})
]);
Why am I getting this error? Isn't the Router injectable when at the time of ExceptionHandler instantiation?
The complete source code is available here
https://github.com/harindaka/angular2-seed-typescript/tree/b368315ce6608085f3154a03bc53f0404ce16495
Probably late to the party but this works for me (Angular 2 RC4):
And then in your main.ts:
See: ErrorHandler class. You can add
Injectable
decorator to achieve DI too!Note: The above answers use
ExceptionHandler
which is removed in final release in favor ofErrorHandler
.update
ExceptionHandler
was renamed toErrorHandler
https://stackoverflow.com/a/35239028/217408orgiginal
I guess this is caused of a circular dependency. You can work around by injecting the injector and get the router imperatively:
just had a similar problem and solved it like this (using that in the providers array of the app module):
So I ran into something similar today. My situation is a little different, I extended Http, which needed Router. However, the ErrorHandler also needed Http. Using the method above with Factories, I thought I could just inject Http into ErrorHandler. I found that when ErrorHandler invoked the Dependency Injection in the constructor for Http, Router did not exist (nor did it have all the other needed context).
So I have the injector get me an instance during the function call, and not in the constructor. Which when the injector actually tries to get the Http (in the call) it has already be created within the proper context.
CustomErrorHandler.ts:
Relevant parts from CustomHttpService.ts:
app.module.ts:
That's a pretty easy one - to inject into a class, you need to make the class
injectable
. Sounds familiar, right?If you add a
@Injectable()
at the top of yourErrorHandler
class, it should work.