Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No provider for RouterOutletMap

Tags:

angular

Newest Angular 2.0.0 and via newest angular-cli 1.0.0-beta.14, node: 6.6.0, os: linux x64

What I do:

1) Create new project

ng new angular-test
ng g component projects
ng g component typings

2) Add simple routing

/src/app/app.component.html

 <router-outlet></router-outlet>

/src/app/app.module.ts

export const ROUTES: Routes = [
  {
    path: '',
    redirectTo: '/projects',
    pathMatch: 'full'
  },
  {
    path: 'projects',
    component: ProjectsComponent,
  },
  {
    path: '/typings',
    component: TypingsComponent
  },
  {
    path: '**', redirectTo: ''
  }
];

@NgModule({
  declarations: [
    AppComponent,
    ProjectsComponent,
    TypingsComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    RouterModule.forChild(ROUTES)
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

What I get:

EXCEPTION: Error in ./AppComponent class AppComponent - inline template:3:0 caused by: No provider for RouterOutletMap!
ORIGINAL EXCEPTION: No provider for RouterOutletMap!

enter image description here

How I tried to fix this

I tried to add RouterOutletMap to providers in AppModule, exception don't throw, but app don't redirect to projects and don't show nesting components

like image 794
srghma Avatar asked Sep 17 '16 11:09

srghma


1 Answers

You need to call RouterModule.forRoot for the app module, not forChild. The former adds all the core providers, while the latter doesn't. You should use forChild for child modules, not the app module.

like image 67
Paul Samsotha Avatar answered Nov 15 '22 20:11

Paul Samsotha