36

i m using angular 5 latest and i am hitting below exception

ERROR Error: StaticInjectorError(AppModule)[AppComponent -> ActivatedRoute]: 
  StaticInjectorError(Platform: core)[AppComponent -> ActivatedRoute]: 
    NullInjectorError: No provider for ActivatedRoute!
    at _NullInjector.get (core.js:1002)
    at resolveToken (core.js:1300)
    at tryResolveToken (core.js:1242)
    at StaticInjector.get (core.js:1110)
    at resolveToken (core.js:1300)
    at tryResolveToken (core.js:1242)
    at StaticInjector.get (core.js:1110)
    at resolveNgModuleDep (core.js:10854)
    at NgModuleRef_.get (core.js:12087)
    at resolveDep (core.js:12577)

the app.module.ts looks like below import { Routes, RouterModule } from '@angular/router';

@NgModule({
  declarations: [
    AppComponent,
    OptyDetailsComponent,
    GlobalNavbarComponent
  ],
  imports: [
    BrowserAnimationsModule,
    BrowserModule,
    HttpModule,
    HttpClientModule,
    FlexLayoutModule,
    FormsModule,
    MatButtonModule,
    MatInputModule
    RouterModule
  ],
  entryComponents: [
  ], 
  providers: [
    DataStoreService,
    DataObjectsOscService,
    AdobeSignService,
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }
4

1 回答 1

49

为了能够提供ActivatedRoute到您的角度元素,您需要将调用的结果RouterModule.forRoot导入您的根模块(AppModule)。这是因为返回的模块RouterModule.forRoot包括 的实例的提供者ActivatedRoute等等。

所以基本上你需要在根模块的导入中添加以下内容:

@NgModule({
  ...
  imports: [
    ...
    // Remark: because you havent defined any routes, I have to pass an empty
    // route collection to forRoot, as the first parameter is mandatory.
    RouterModule.forRoot([]),
    ...
  ],
  ...
})
export class AppModule { }

ActivatedRoute但老实说,尽管您没有为根模块定义任何路由,但您使用它有点奇怪。

有关详细信息,请参阅:

ActivatedRoute 提供程序源

RouterModule.forRoot() 源码

于 2018-02-28T23:08:44.207 回答