NullInjectorError:没有路由器的提供者!误差角度2

时间:2018-02-05 07:03:29

标签: angular angular2-routing

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)

4 个答案:

答案 0 :(得分:2)

使用以下代码生成模块时遇到了同样的错误:

ng g m nameofModule --routing

这在已创建的模块的@NgModule中导入了以下内容:

imports: [RouterModule.forChild(routes)]

它应该是为了不是为了孩子。应该是:

imports: [RouterModule.forRoot(routes)]

希望这有帮助。

答案 1 :(得分:1)

RouterModule添加到AppModule

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

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

错误指定在项目中没有为Router指定提供程序。要解决此问题,您需要将RouterModule添加到模块中的导出中。

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

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