У меня есть AuthInterceptor
на AppModule
.Он отлично работает на AppModule
, но не работает, когда дело доходит до LazyModule
.Я должен был предоставить это interceptor
для работы всех Lazy Module.
Я действительно удивлен, почему это так.Я реализовал то же самое в других проектах, которые прекрасно работают, но здесь я должен был предоставить в каждом модуле.Не уверен, что является причиной проблемы.
AppModule
@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 {
}
AppRoutingModule
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 {
}
PagesModule
@NgModule({
imports: [
PagesRoutingModule,
ThemeModule,
Dashboard2Module
],
declarations: [
PagesComponent,
],
})
export class PagesModule {
}
PagesRoutingModule
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 {
}
ProfileModule
@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 {}