Shawn Shawn - 1 year ago 253
TypeScript Question

(SystemJS) Can't resolve all parameters for Router in Angular 2.1.2

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';

selector: 'login',
templateUrl: 'app/login/auth.login.html',
styleUrls: ['app/login/auth.login.css'],
providers: [FormBuilder, AuthService, { provide: Router, useClass: RouterModule} ]
export class LoginComponent {
form: FormGroup;

constructor(fb: FormBuilder,
private authSvc: AuthService,
private router: Router
) {....}

In app.routing.module.ts I have

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

Then I am getting error as follows

zone.js@0.6.26?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
in root component and everything is taken care of. How do I use RouterModule to make Router resolved by the compiler?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download