angular - 为什么必须为每个惰性模块提供 HTTP_INTERCEPTORS
问题描述
我AuthInterceptor
在AppModule
. 它工作正常,AppModule
但在LazyModule
. 我必须interceptor
为所有 Lazy Module 提供此功能才能正常工作。
我真的很惊讶为什么会这样。我在其他项目中实现了同样的东西,效果很好,但在这里我必须在每个模块中提供。不知道是什么导致了这个问题。
应用模块
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
BrowserAnimationsModule,
HttpClientModule,
AppRoutingModule,
//NgbModule.forRoot(),
ThemeModule.forRoot(),
CoreModule.forRoot(),
],
bootstrap: [AppComponent],
providers: [
{provide: APP_BASE_HREF, useValue: '/'},
{provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true},
AuthGuard,
{
provide: NbRoleProvider,
useClass: RoleProvider,
},
ZtLoaderService
],
})
export class AppModule {
}
应用路由模块
const routes: Routes = [
{
path: 'pages',
canActivate: [AuthGuard], // here we tell Angular to check the access with our AuthGuard
loadChildren: 'app/pages/pages.module#PagesModule'
},
{path: '', redirectTo: 'pages', pathMatch: 'full'},
{path: '**', redirectTo: 'pages'},
];
const config: ExtraOptions = {
useHash: true,
};
@NgModule({
imports: [RouterModule.forRoot(routes, config)],
exports: [RouterModule],
})
export class AppRoutingModule {
}
页面模块
@NgModule({
imports: [
PagesRoutingModule,
ThemeModule,
Dashboard2Module
],
declarations: [
PagesComponent,
],
})
export class PagesModule {
}
页面路由模块
const routes: Routes = [{
path: '',
component: PagesComponent,
children: [{
path: 'dashboard',
component: DashboardComponent2,
},
{
path: 'profile',
loadChildren: './profile/profile.module#ProfileModule',
}],
}];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
})
export class PagesRoutingModule {
}
配置文件模块
@NgModule({
imports: [
ThemeModule,
ProfileRoutingModule,
Ng2SmartTableModule,
],
declarations: [
...routedComponents,
...components
],
providers: [{provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true}], //<--- Interceptor is provided here. Could be avoided.
})
export class ProfileModule {}
解决方案
推荐阅读
- prolog - 在 Eclipse Prolog 中使用“not”运算符
- asp.net - 从 api 获取 json (flutter web)
- python - 从递归辅助函数产生
- r - 如何在 R 的边缘图中绘制直方图轴?
- python - 在 pygame 中,当我与某物碰撞并使其保持不变时,如何对图像进行 blit 处理?
- linux - 如何使用 CMAKE 从 Linux 交叉编译 LuaJIT for Windows?
- selenium - 使用机器人框架查找 shadow dom 元素的 xpath
- microsoft-graph-api - 为 OneDrive Business 创建订阅返回 403 禁止
- codenameone - java.lang.OutOfMemoryError loading Images in Codename One
- c# - 在foreach之后获取提交按钮的信息