我写了一个简单的拦截器“NoopInterceptorService”
但我得到一个错误“没有NoopInterceptorService的提供者!” ,
可能是什么原因?
如何解决?
错误:NoopInterceptorService没有提供程序!角4.3.6?
回答
这LINK应该帮助如何编写拦截器。
import { Observable } from 'rxjs';
import {Injectable} from '@angular/core';
import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest} from '@angular/common/http';
import { HttpErrorResponse } from "@angular/common/http";
@Injectable()
export class AngularInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).do(event => {}, err => {
if(err instanceof HttpErrorResponse){
console.log("Error Caught By Interceptor");
//Observable.throw(err);
}
});
}
}
撑着在
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule
],
providers: [
[ { provide: HTTP_INTERCEPTORS, useClass:
AngularInterceptor, multi: true } ]
],
bootstrap: [AppComponent]
})
export class AppModule {
}
当我添加提供“HTTP_INTERCEPTORS”时,我会收到一个错误“NoopInterceptorService没有提供者!” {提供:HTTP_INTERCEPTORS,useClass:AngularInterceptor,multi:true} – toney
首先,为了实现拦截器,你已经使用HttpClientModule
而不是HttpModule
任何HTTP调用。
现在,执行:
在你App.module.ts文件,导入:
import {HttpClientModule, HTTP_INTERCEPTORS} from '@angular/common/http';
和你的进口数组中AppModule
添加以下内容:
imports: [
.
HttpClientModule,
.
]
您的提供商阵列和NoopInterceptorService
实施似乎是正确的。但是,重要的是,无论何时您将在任何组件代码中使用http方法,请不要忘记使用http方法HttpClientModule
而不是旧的HttpModule
。
我想如果你遵循这个,你的INTERCEPTORS将会工作。
这就是我写的,但是我得到一个错误“NoopInterceptorService没有提供者!” – toney
- 1. 4角(角CLI)错误:没有提供程序的翻译:
- 2. 错误:没有配置提供程序
- 3. 错误:没有提供程序ValidateService
- 4. 错误:没有提供程序AuthInterceptor
- 5. 错误:没有提供程序组件
- 6. 没有提供MdDialog错误
- 7. 没有Http的提供商!错误:没有提供程序的Http!在注入错误
- 8. 角2自定义HTTP服务引发错误:没有提供程序XHRBackend
- 9. 错误:在Ionic2中没有为MediaCapture提供程序
- 10. 休眠JPA - 没有EntityManager错误的持久性提供程序
- 11. 使用ngControl造成错误:没有提供程序ControlContainer
- 12. 获取错误“没有AlertIconAndTypesService提供程序!”
- 13. Footable错误:错误:没有列提供的,没有显示表
- 14. 角2服务错误 - 没有提供对HTTP
- 15. 角度2错误没有“BillingService”的提供者
- 16. 测量角服务提供了错误:没有模块:ngResource
- 17. 角度开放$ uibModal给未知的提供程序错误
- 18. NG引导程序模式组件没有提供程序错误
- 19. 23andMe API错误:'没有提供grant_type。' 'invalid_request'OAuth2
- 20. 错误:没有提供“框架:协议”
- 21. 错误:没有JwtHelper的提供者
- 22. 错误:没有NavParams的提供商
- 23. Angular 2 - 没有提供程序错误(尽管我已经添加了提供程序)
- 24. 错误:在angular2中没有提供HttpHandler的提供者
- 25. 在Injection Error处没有服务提供商,在没有提供商错误
- 26. EFCore 1.1 Scaffold-DbContext提供程序错误
- 27. HibernatePersistence提供程序发生错误
- 28. AngularJS:未知的提供程序错误
- 29. sql compact会员提供程序错误
- 30. 用户提供程序错误symfony
你的根模块进口HttpClientModule? –