Angular 2 RC5: No Provider for Router

I ran into a problem using the new Angular 2 RC5 router (version of the RC1 router).

Here is the log I get from the dev console:

EXCEPTION: Error in /templates/app.component.html:2:0 ORIGINAL EXCEPTION: No provider for Router! 

This is what my app.modules.ts looks like:

 import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule } from '@angular/forms'; import { HttpModule } from '@angular/http'; import { RouterModule } from '@angular/router'; import { AppComponent } from './components/app.component'; import { routing } from './app.routes'; @NgModule({ declarations: [AppComponent], imports: [BrowserModule, RouterModule, FormsModule, HttpModule, routing], bootstrap: [AppComponent], }) export class AppModule {} 

Here is my boot.ts file:

 import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app.module'; platformBrowserDynamic().bootstrapModule(AppModule); 

My app.routes.ts file ...

 import { Routes, RouterModule } from '@angular/router'; // Components import { HomeComponent } from './components/home.component'; const routes: Routes = [ // Root { path: '/', name: 'Home', component: HomeComponent, useAsDefault: true}, ]; // - Updated Export export const routing = RouterModule.forRoot(routes); 

... and the app.component.ts file:

 import { Component, ViewContainerRef } from '@angular/core'; @Component({ selector: 'app', templateUrl: '/templates/app.component.html' }) export class AppComponent { viewContainerRef: any; public constructor(viewContainerRef:ViewContainerRef) { // You need this small hack in order to catch application root view container ref this.viewContainerRef = viewContainerRef; } } 

Is there something I'm missing here?

+8
javascript angularjs typescript routing
source share
1 answer

Inside the app.routes.ts file:

Add this import status

 import { ModuleWithProviders } from '@angular/core'; 

Than

 export const routing: ModuleWithProviders = RouterModule.forRoot(routes); 

That should work.

+9
source share

All Articles