Angular 如何仅为子模块导入角度HTTP侦听器
我已经在AppModule中导入了HttpClientModuleAngular 如何仅为子模块导入角度HTTP侦听器,angular,http,httpclient,interceptor,angular-http-interceptors,Angular,Http,Httpclient,Interceptor,Angular Http Interceptors,我已经在AppModule中导入了HttpClientModule import { HttpClientModule } from '@angular/common/http'; export const AppRoutes: Routes = [ { path: '', redirectTo: 'dashboard', pathMatch: 'full' }, { path: 'account', loadChildren: './auth/auth.module#
import { HttpClientModule } from '@angular/common/http';
export const AppRoutes: Routes = [
{ path: '', redirectTo: 'dashboard', pathMatch: 'full' },
{
path: 'account',
loadChildren: './auth/auth.module#AuthModule'
},
{
path: 'dashboard',
loadChildren: './content/content.module#ContentModule'
}
];
一旦我启动这个站点,它将重定向到仪表板模块(惰性加载模块),我在其中添加了http拦截器
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { HttpInterceptorService } from '../services/http-interceptor.service';
@NgModule({
providers : [
{ provide: HTTP_INTERCEPTORS, useClass: HttpInterceptorService, multi: true }
]
})
但它不起作用。有人能帮我吗?Http拦截器只是一种服务,在任何模块上注入它没有任何区别-只要在
AppModule
中执行同样的操作,这将帮助您工作-如果您遇到同样的问题,您可以尝试在共享模块上注入如下内容
实现这一点的常见模式不是直接在@NgModule
声明中公开提供程序,而是在一个静态forRoot函数中,如下所示:
export class SharedModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: SharedModule,
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: HttpInterceptorService, multi: true }
]
};
}
}
然后,您可以在AppModule
上导入您的SharedModule
,如SharedModule.forRoot()
希望这能起作用-快乐编码post-appmodule感谢您的努力。但事实上,过一段时间它就可以工作了。没问题:)快乐编码