Goal
Get routing working without losing sanity.
Error
Error: Can't resolve all parameters for Router: (?, ?, ?, ?, ?, ?, ?, ?)
app.routing.ts
import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { NavbarComponent } from './navbar/navbar.component';
import { CustomerComponent } from './customer/customer.component';
export const appRoutes: Routes = [
{ path: '', redirectTo: 'customers', pathMatch: 'full' },
{ path: 'customers', component: CustomerComponent },
];
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);
app.module.ts
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { routing } from './app.routing';
import { AppComponent } from './app.component';
import { NavbarComponent } from './navbar/navbar.component';
import { CustomerComponent } from './customer/customer.component';
@NgModule({
imports: [
BrowserModule,
FormsModule,
routing
],
declarations: [
AppComponent,
NavbarComponent,
CustomerComponent,
],
providers: [
// ...
],
bootstrap: [ AppComponent ]
})
export class AppModule {
// ...
}
app.component.ts
import { RouterLink } from '@angular/router';
import { HTTP_PROVIDERS } from '@angular/http';
import { Component } from '@angular/core';
import { NavbarComponent } from './navbar/navbar.component';
import { CustomerComponent } from './customer/customer.component';
export { Config } from './config/env.config';
@Component({
moduleId: module.id,
selector: 'app',
templateUrl: 'app.component.html',
styleUrls: ['app.component.css'],
directives: [NavbarComponent, CustomerComponent],
providers: [HTTP_PROVIDERS, RouterLink]
})
export class AppComponent
{
constructor() {
// console.log('Environment config', Config);
}
ngOnInit() {
// ...
}
}
navbar.component.ts
import { Router, ROUTER_DIRECTIVES } from '@angular/router';
import { Component } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'navbar',
templateUrl: 'navbar.component.html',
styleUrls: ['navbar.component.css'],
directives: [ROUTER_DIRECTIVES],
providers: [Router],
})
export class NavbarComponent
{
version: string;
versionIsVisible: boolean;
constructor() {
this.version = '<%= VERSION %>';
}
ngOnInit() {
// ...
}
}
app.component.html
<navbar></navbar>
<router-outlet></router-outlet>
navbar.component.html
<a routerLink="/customers">Customers</a>
Appreciate older post now but I had the same issue and have just resolved it.
The error advises Angular cannot resolve some of the dependencies of the Router class.
navbar.component.ts
import { Router, ROUTER_DIRECTIVES } from '@angular/router';
import { Component } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'navbar',
templateUrl: 'navbar.component.html',
styleUrls: ['navbar.component.css'],
directives: [ROUTER_DIRECTIVES],
providers: [Router],
})
I fixed this by not injecting Router as a provider, and only injecting into the constructor so you end up with:
@Component({
moduleId: module.id,
selector: 'navbar',
templateUrl: 'navbar.component.html',
styleUrls: ['navbar.component.css'],
directives: [ROUTER_DIRECTIVES]
})
export class NavbarComponent
{
version: string;
versionIsVisible: boolean;
constructor(private _router: Router) {
this.version = '<%= VERSION %>';
}
ngOnInit() {
// ...
}
}
I'm new to angular2 so I'm not able to give you a detailed reason why!
Here is some code from a personal project I have worked on using Angular 2 RC5 and that has routers working. I used the information straight from the documentation so maybe it will be helpful to you.
app.routing.ts
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from '../home/home.component';
import { ApprovalsComponent } from '../approvals/approvals.component';
const appRoutes : Routes = [
{
path: 'Home', component: HomeComponent ,
data: { Title: 'Home'}
},
{
path: 'Approvals', component: ApprovalsComponent,
data: { Title: 'My Approvals'}
}]
export const appRoutingProviders: any[] = [
];
export const routing = RouterModule.forRoot(appRoutes);
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http'
import { AppComponent } from './app.component';
import { HomeComponent } from '../home/home.component'
import { ApprovalsComponent } from '../approvals/approvals.component'
import { routing, appRoutingProviders } from './app.routing'
@NgModule({
imports: [ BrowserModule, routing, HttpModule, BrowserModule],
declarations: [ AppComponent, HomeComponent, ApprovalsComponent],
providers : [appRoutingProviders],
bootstrap: [ AppComponent ]
})
export class AppModule { }
html for routerlink
<a [routerLink]="['Home']">
Home
</a>
Do you have your router outlet declared somewhere? In my app.html I have:
<router-outlet></router-outlet>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With