Angular 2: Can't resolve all parameters for Router -
goal
get routing working without losing sanity.
error
error: can't resolve parameters router: (?, ?, ?, ?, ?, ?, ?, ?)
app.routing.ts
import { modulewithproviders } '@angular/core'; import { routes, routermodule } '@angular/router'; import { navbarcomponent } './navbar/navbar.component'; import { customercomponent } './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 } '@angular/core'; import { formsmodule } '@angular/forms'; import { browsermodule } '@angular/platform-browser'; import { routing } './app.routing'; import { appcomponent } './app.component'; import { navbarcomponent } './navbar/navbar.component'; import { customercomponent } './customer/customer.component'; @ngmodule({ imports: [ browsermodule, formsmodule, routing ], declarations: [ appcomponent, navbarcomponent, customercomponent, ], providers: [ // ... ], bootstrap: [ appcomponent ] }) export class appmodule { // ... }
app.component.ts
import { routerlink } '@angular/router'; import { http_providers } '@angular/http'; import { component } '@angular/core'; import { navbarcomponent } './navbar/navbar.component'; import { customercomponent } './customer/customer.component'; export { config } './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 } '@angular/router'; import { component } '@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 had same issue , have resolved it.
the error advises angular cannot resolve of dependencies of router class.
navbar.component.ts
import { router, router_directives } '@angular/router'; import { component } '@angular/core'; @component({ moduleid: module.id, selector: 'navbar', templateurl: 'navbar.component.html', styleurls: ['navbar.component.css'], directives: [router_directives], providers: [router], })
i fixed not injecting router provider, , injecting constructor end 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 angular2 i'm not able give detailed reason why!
Comments
Post a Comment