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)
答案 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 {}