如何将拦截器和服务从应用程序模块加载到延迟加载的模块?

时间:2019-02-06 06:46:26

标签: angular angular6

I have loaded some interceptors and services in app.module.ts and want to 
load same interceptors and services in lazy loaded module.how do i load 
it?This is app.module.ts file where i have loaded interceptors and 
services in providers.
@NgModule({

providers: [
{ provide: APP_BASE_HREF, useValue: '/' },
{ provide: HTTP_INTERCEPTORS, useClass: LoaderInterceptor, multi: true },
{ provide: HTTP_INTERCEPTORS, useClass: CommonInterceptor, multi: true },
{ provide: HTTP_INTERCEPTORS, useClass: ServerErrorsInterceptor, multi: 
true },
CommonServiceService, 
dialogModelService, 
LoaderService, 
LoginServiceService, 
MessageService,
ValidationService,
AuthGuard,
{ provide: USER_STORAGE, useExisting: SESSION_STORAGE },
    UserStorageService
],
})

export class AppModule {
}

当内部模块加载到pages文件夹中时。拦截器和服务在延迟加载的模块中不起作用

enter image description here

1 个答案:

答案 0 :(得分:0)

app.module.ts中的所有服务provided应该具有全局作用域,这意味着,您可以将它们用于任何延迟加载的模块中的依赖项注入,而不会在整个应用程序中出现错误

如果您想为延迟加载的模块提供单独的提供程序实例,只需将其添加到相应@NgModule的providers数组中