角度错误:没有提供ActivatedRoute的提供程序 [英] Angular error: no provider for ActivatedRoute

查看:77
本文介绍了角度错误:没有提供ActivatedRoute的提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用最新的角度5,并且在异常以下达到目标

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)

app.module.ts如下所示 从"@ angular/router"导入{Routes,RouterModule};

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

推荐答案

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

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

但是,老实说,尽管您尚未为根模块定义任何路由,但您还是使用ActivatedRoute还是很奇怪的.

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提供程序源

RouterModule.forRoot()源

这篇关于角度错误:没有提供ActivatedRoute的提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆