Aashiq Rathnadas
Aashiq Rathnadas

Reputation: 525

NullInjectorError: No provider for Router! error angular 2

ERROR Error: StaticInjectorError(AppModule)[HeaderComponent -> Router]: 
  StaticInjectorError(Platform: core)[HeaderComponent -> Router]: 
    NullInjectorError: No provider for Router!
    at _NullInjector.get (core.js:994)
    at resolveToken (core.js:1292)
    at tryResolveToken (core.js:1234)
    at StaticInjector.get (core.js:1102)
    at resolveToken (core.js:1292)
    at tryResolveToken (core.js:1234)
    at StaticInjector.get (core.js:1102)
    at resolveNgModuleDep (core.js:10836)
    at NgModuleRef_.get (core.js:12069)
    at resolveDep (core.js:12559)

Upvotes: 0

Views: 12864

Answers (4)

Esco
Esco

Reputation: 51

I was having the same error when I generated my module using:

ng g m nameofModule --routing

This imported the following in @NgModule of the created Module:

imports: [RouterModule.forChild(routes)]

It's supposed to be forRoot not forChild. Its supposed to be :

imports: [RouterModule.forRoot(routes)]

Hope this helps.

Upvotes: 5

gastonche
gastonche

Reputation: 463

The error specifies that no provider has been specified for Router in your project. To solve this you will need to add RouterModule to the exports in your module.

import { NgModule }             from '@angular/core';
import { RouterModule } from '@angular/router';

@NgModule({
  exports: [ RouterModule ]
})
export class AppRoutingModule {}

Upvotes: 0

Nour
Nour

Reputation: 5889

 Import {RouterModule} from '@angular/router';

 @NgModule({
   imports: [
     RouterModule
   ],
})

Upvotes: -1

Suren Srapyan
Suren Srapyan

Reputation: 68655

Add RouterModule into your AppModule.

import { RouterModule } from '@angular/router' ;

NgModule({
   imports: [
      ...
      RouterModule.forRoot(your routes here)
      ...
   ]
})
export class AppModule

Upvotes: 1

Related Questions