Tob*_*ter 5 httpclient interceptor typescript angular
我有一个拦截器用于http get/post请求.它工作得很好.
不幸的是,它不适用于我的jsonp请求/它们没有被捕获.
因此,我想创建另一个实现JsonpInterceptor的拦截器.
事实上,它也不起作用.
到目前为止我做了什么:
的AppModule:
[{ provide: HTTP_INTERCEPTORS, useClass: MyJsonpInterceptor, multi: true }],
Run Code Online (Sandbox Code Playgroud)
MyJsonpInterceptor:
@Injectable()
export class MyJsonpInterceptor implements JsonpInterceptor {
constructor(jsonp: JsonpClientBackend) {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log("intercept");
}
}
Run Code Online (Sandbox Code Playgroud)
有人知道,如何用新的httpclient拦截角度为4/5的jsonp请求?
小智 1
我认为JsonpInterceptor角度也许是关键点。这是我的解决方案:
NgModule配置,删除HttpClientJsonpModule.JsonpClientBackend和添加{provide: \xc9\xb5b, useFactory: \xc9\xb5c}到提供程序。\xc9\xb5b & \xc9\xb5c 是JsonpCallbackContextand的别名jsonpCallbackContext{provide: HTTP_INTERCEPTORS, useClass: JsonpInterceptor, multi: true},为最后一个HTTP_INTERCEPTORS。或者您可以使用自定义拦截器覆盖 JsonpInterceptor。希望这可以帮到你。
\n\n2018年3月9日更新:
\n\n你可以简单地这样做:
\n\n@NgModule({\n providers: [\n { provide: HTTP_INTERCEPTORS, useClass: XxxInterceptor, multi: true },\n ],\n})\nexport class XxxModule {\n}\n\n@NgModule({\n declarations: [\n ],\n imports: [\n XxxModule,\n HttpClientJsonpModule,\n ],\n providers: [\n ...\n ],\n bootstrap: [AppComponent]\n})\nexport class AppModule { }\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
956 次 |
| 最近记录: |