0

I am upgrading from RC4 to 2.1.2. I understand that ROUTER_DIRECTIVES is deprecated. So in my LoginComponent now looks like this

import {Component,Injectable} from '@angular/core'; import {Router,RouterModule} from '@angular/router'; import {FormBuilder, Validators, FormGroup, FormsModule} from '@angular/forms'; import {AuthService} from './auth.service'; @Component({ selector: 'login', templateUrl: 'app/login/auth.login.html', styleUrls: ['app/login/auth.login.css'], providers: [FormBuilder, AuthService, { provide: Router, useClass: RouterModule} ] }) @Injectable() export class LoginComponent { form: FormGroup; constructor(fb: FormBuilder, private authSvc: AuthService, private router: Router ) {....} 

In app.routing.module.ts I have

@NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule], providers: [ AuthGuard, ] }) export class AppRoutingModule { } 

Then I am getting error as follows

[email protected]?main=browser:232 Error: (SystemJS) Can't resolve all parameters for Router: (?, ?, ?, ?, ?, ?, ?, ?).(…) 

I have tried moved { provide: Router, useClass: RouterModule} to app.routing.module.ts but it didn't help.

In RC4 we used directives: [ROUTER_DIRECTIVES] in root component and everything is taken care of. How do I use RouterModule to make Router resolved by the compiler?

4
  • 2
    After you remove { provide: Router, useClass: RouterModule} it should work, given you added the AppRoutingModule to the app module imports. Other than that, please try and provide a Plunker for better help. Commented Nov 13, 2016 at 23:44
  • I started without { provide: Router, useClass: RouterModule} and got the error. Then I looked at stackoverflow.com/questions/39015918/… and began adding { provide: Router, useClass: RouterModule} but it didn't help either. So by removing { provide: Router, useClass: RouterModule} I would still be getting the error. It must be something else I may have missed? Commented Nov 13, 2016 at 23:48
  • 1
    Thanks for pointing that out so I can down vote it :-) That is not a solution. You need to have a little better understanding of DI to realize that is a dufus idea. Like I said, it should work once you import the AppRoutingModule into the app module. Can you provide a Plunker that reproduces the problem? Commented Nov 13, 2016 at 23:53
  • I think you are right. I removed it and the error went away. thanks:) Commented Nov 14, 2016 at 0:19

1 Answer 1

1

Just import RouterModule.forRoot(routes) in AppModule and RouterModule in other modules using Router. Don't put into component's providers, AppRoutingModule also seems unnecessary.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.