如何向Angular 4应用程序添加多个独立HTTP拦截器?将多个HTTP拦截器添加到Angular应用程序
我试图通过扩展providers
数组并添加多个拦截器来添加它们。但只有最后一个实际执行,Interceptor1
被忽略。
@NgModule({
declarations: [ /* ... */ ],
imports: [ /* ... */ HttpModule ],
providers: [
{
provide: Http,
useFactory: (xhrBackend: XHRBackend, requestOptions: RequestOptions) =>
new Interceptor1(xhrBackend, requestOptions),
deps: [XHRBackend, RequestOptions],
},
{
provide: Http,
useFactory: (xhrBackend: XHRBackend, requestOptions: RequestOptions) =>
new Interceptor2(xhrBackend, requestOptions),
deps: [XHRBackend, RequestOptions]
},
],
bootstrap: [AppComponent]
})
export class AppModule {}
我能明显它们合并成一个单一的Interceptor
类,并应工作。但是,我想避免这种情况,因为这些拦截器有完全不同的目的(一个用于错误处理,一个用于显示加载指示器)。
那么如何添加多个拦截器?
你正在重写'Http'。只使用最后的覆盖。 Interceptor1不被忽略,它只是不存在。您可以使用包含拦截器的HttpClient。 – estus
@estus你的意思是“你可以使用包含拦截器的HttpClient”。 – str
https://angular.io/guide/http – estus