我理解了anguler拦截器是根据它们在模块中的声明顺序执行的。
@NgModule({
imports: [...],
declarations: [...],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: FirstInterceptor,
multi: true,
},
{
provide: HTTP_INTERCEPTORS,
useClass: SecondInterceptor,
multi: true,
}
]
})
export class AppModule {
constructor() {
}
}
FirstInterceptor将在SecondInterceptor之前执行。
但是,当拦截器位于导入的模块中时,顺序如何?
@NgModule({
declarations: [...],
imports: [...],
exports: [...],
})
export class ImportedModule{
static forRoot() {
return {
ngModule: ImportedModule,
providers: [
...,
{
provide: HTTP_INTERCEPTORS,
useClass: ThirdInterceptor,
multi: true,
}
],
};
}
}
@NgModule({
imports: [
...,
ImportedModule.forRoot(),
],
declarations: [...],
providers: [
...
{
provide: HTTP_INTERCEPTORS,
useClass: FirstInterceptor,
multi: true,
},
{
provide: HTTP_INTERCEPTORS,
useClass: SecondInterceptor,
multi: true,
}
]
})
export class AppModule {
constructor() {
}
}
根据我的测试,导入的模块拦截器(ThirdInterceptor)在AppModule拦截器之后执行。
在我的情况下,这不是预期的行为,我希望首先执行导入的模块拦截器,这可能吗?