【发布时间】:2017-07-17 01:53:45
【问题描述】:
我正在尝试使用 Angular 4.3 中的新 HttpClient 实现一个 http 拦截器,但我不断收到错误消息:
ERROR 错误:未捕获(承诺中):错误:没有 AuthInterceptor 的提供者!
这就是我的 app.module.ts 中的内容:
@NgModule({
bootstrap: [AppComponent],
declarations: [
AppComponent,
LoginComponent,
NotFoundComponent
],
imports: [
RouterModule.forRoot([
{ path: "", redirectTo: "login", pathMatch: "full" },
{ path: "login", component: LoginComponent },
{ path: "**", component: NotFoundComponent }
]),
NgbModule.forRoot(),
BrowserAnimationsModule,
HttpClientModule,
],
providers: [
AuthenticationService,
{ provide: HTTP_INTERCEPTORS, useExisting: AuthInterceptor, multi: true },
]
};
我查看了所有带有错误“没有 CustomService 提供者”的文章,它们都提到确保将服务添加到 app.module 中的提供者列表中,我认为我已经完成了。
还有人有其他想法吗?
谢谢。
【问题讨论】:
-
你为什么使用
useExisting?那是为了给现有的依赖令牌起别名。除非某些东西已经在令牌AuthInterceptor下提供了某些东西,否则它将失败。尝试将AuthInterceptor添加到您的providers属性中。 -
啊,你是对的。我遵循了关于实现拦截器的教程,但没有意识到 useExisting 意味着它必须已经存在。如果您提交评论作为问题的答案,我很乐意将其标记为正确答案。
标签: angular