Angular error: no provider for ActivatedRoute

▼魔方 西西 提交于 2019-11-30 14:30:59

问题


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 { }

回答1:


For being able to provide ActivatedRoute into your angular elements, you need to import the result of calling RouterModule.forRoot into your root module (AppModule). This is because the module returned by RouterModule.forRoot includes the provider for instances of ActivatedRoute, among others.

So basically you need to add the following to your imports in the root module:

@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 { }

But to be honest, its kinda odd that you use ActivatedRoute, despite the fact that you haven't defined any routes for your root module.

For further details see:

ActivatedRoute provider source

RouterModule.forRoot() source



来源:https://stackoverflow.com/questions/49039457/angular-error-no-provider-for-activatedroute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!