简体   繁体   English

NullInjectorError:没有路由器的提供程序! 误差角2

[英]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)

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: 这将以下内容导入到创建的模块的@NgModule中:

imports: [RouterModule.forChild(routes)]

It's supposed to be forRoot not forChild. 应该是forRoot而不是forChild。 Its supposed to be : 它应该是:

imports: [RouterModule.forRoot(routes)]

Hope this helps. 希望这可以帮助。

Add RouterModule into your AppModule . RouterModule添加到您的AppModule

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

NgModule({
   imports: [
      ...
      RouterModule.forRoot(your routes here)
      ...
   ]
})
export class AppModule
 Import {RouterModule} from '@angular/router';

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

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. 为了解决这个问题,您需要将RouterModule添加到模块中的导出中。

 import { NgModule } from '@angular/core'; import { RouterModule } from '@angular/router'; @NgModule({ exports: [ RouterModule ] }) export class AppRoutingModule {} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 6 错误“NullInjectorError:没有路由器提供程序!” - Angular 6 Error "NullInjectorError: No provider for Router!" 错误:NullInjectorError:没有路由器提供程序 - error:NullInjectorError: No provider for Router Angular 2错误NullInjectorError:没有Http的提供程序 - Angular 2 error NullInjectorError: No provider for Http 如何使用角度模块? (NullInjectorError:没有路由器的提供程序) - How to use angular modules? (NullInjectorError: No provider for Router) 角度测试错误 - NullInjectorError:没有TrimInputDirective的提供者 - Angular Test Error - NullInjectorError: No provider for TrimInputDirective Quill Angular 错误:NullInjectorError: No provider for InjectionToken config - Quill Angular Error: NullInjectorError: No provider for InjectionToken config Angular 单元测试 NullInjectorError:没有 HttpClient 的提供者! 错误 - Angular Unit Tests NullInjectorError: No provider for HttpClient! Error 获取 NullInjectorError 的错误:没有 HttpClient 的提供者! 在 angular 测试 - Getting the error of NullInjectorError: No provider for HttpClient! in angular testing Angular 9 测试错误 NullInjectorError: No provider for Window - Angular 9 test error NullInjectorError: No provider for Window 错误:StaticInjectorError(DynamicTestModule)[RouterLinkWithHref -> Router]:(NullInjectorError:没有路由器的提供者!) - Error: StaticInjectorError(DynamicTestModule)[RouterLinkWithHref -> Router]: (NullInjectorError: No provider for Router!)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM