Search code examples
javascriptangularangular-routingangular-providers

No Provider for router in Angular 2


As a newbie, I am having issues setting up routes in Angular 2. Though I am pretty sure, it is something very basic I am missing.

All I want is my App Component to load up and show me the title. And just below it, a link that would load up the contact-list component in the router-outlet section.

import { Component,OnInit } from '@angular/core';
import { Contact } from './contact';

@Component({
  selector: 'app-root',
  template: `
    <h1>{{title}}</h1>
    <a routerLink="/contact-list">Contact List</a>
    <router-outlet></router-outlet>
    `
})
export class AppComponent{
  title = 'Welcome to my Dream App!';
}

Here is the app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule } from '@angular/router';

import { AppComponent } from './app.component';
import { ContactCardComponent } from './contact-card.component';
import { ContactsListComponent } from './contacts-list.component';

RouterModule.forRoot([
  {
    path:'',
    redirectTo:'app',
    pathMatch:'full',
  },
  {
    path:'app',
    component:AppComponent
  },
  {
    path:'contact-list',
    component:ContactsListComponent
  },
  {
    path:'contact-details/:id',
    component:ContactCardComponent
  }
]);

@NgModule({
  declarations: [
    AppComponent,
    ContactsListComponent,
    ContactCardComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    RouterModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

But is says:

error_handler.js:54 EXCEPTION: Error in ./AppComponent class AppComponent - inline template:2:4 caused by: No provider for Router!

No Provider for Router.

What am I doing wrong ?

UPDATE:

If I do :

  template: `
    <h1>{{title}}</h1>
    <contacts-list></contacts-list>
    `

in app.component.ts, it works fine.

After the solution posted, I see repeated lines:

enter image description here


Solution

  • You should import RouterModule into your module definition and "imports" array:

    const ROUTES = [
      // HERE ROUTES DEFINITIONS
    ]
    
    @NgModule({
        imports: [
            RouterModule.forChild(ROUTES)
        ],
        declarations: [],
        providers: []
    })
    export default class YourModuleDefinition{
    
    }
    

    So your code should look like that:

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { FormsModule } from '@angular/forms';
    import { HttpModule } from '@angular/http';
    import { RouterModule } from '@angular/router';
    
    import { AppComponent } from './app.component';
    import { ContactCardComponent } from './contact-card.component';
    import { ContactsListComponent } from './contacts-list.component';
    
    @NgModule({
       declarations: [
         AppComponent,
         ContactsListComponent,
         ContactCardComponent
       ],
       imports: [
         BrowserModule,
         FormsModule,
         HttpModule,
         RouterModule.forRoot([
           {
              path:'',
              redirectTo:'app',
              pathMatch:'full',
           },
           {
              path:'app',
              component:AppComponent
           },
           {
              path:'contact-list',
              component:ContactsListComponent
           },
           {
              path:'contact-details/:id',
              component:ContactCardComponent
            }
         ]);
       ],
       providers: [],
       bootstrap: [AppComponent]
     })
     export class AppModule { }
    

    The forRoot static class method of the RouterModule with the provided configuration and added to the imports array provides the routing concerns for the module.

    More info here: https://angular.io/guide/ngmodule