gpt4 book ai didi

Angular 错误 : no provider for ActivatedRoute

转载 作者:太空狗 更新时间:2023-10-29 16:51:33 24 4
gpt4 key购买 nike

我正在使用最新的 Angular 5,并且遇到以下异常

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

@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 提供到您的 Angular 元素中,您需要将调用 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 provider source

RouterModule.forRoot() source

关于 Angular 错误 : no provider for ActivatedRoute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49039457/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com