Angular:从惰性功能模块添加多提供者 [英] Angular: add a multi provider from lazy feature module
问题描述
我有一个 ErrorModule (eager) 配置如下:
I have an ErrorModule (eager) configured as follows:
export const CONFIG = new InjectionToken<ErrorConfig[]>('Module errors configuration.');
@NgModule({
imports: [... ]
})
export class ErrorModule {
static forRoot(config: ErrorConfig): ModuleWithProviders {
return {
ngModule: ErrorModule,
providers: [
ErrorService,
{ provide: HTTP_INTERCEPTORS, useClass: ErrorInterceptor, multi: true },
{ provide: CONFIG, useValue: config, multi: true }
]
};
}
static forChild(config: ErrorConfig): ModuleWithProviders {
return {
ngModule: ErrorModule,
providers: [
{ provide: CONFIG, useValue: config, multi: true }
]
};
}
}
核心模块然后按如下方式导入ErrorModule
:
Core module then imports ErrorModule
as follows:
@NgModule({
imports: [
ErrorModule.forRoot(ERROR_CONFIG)
], ...
懒惰加载的子功能模块:
@NgModule({
imports: [
ErrorModule.forChild(ERROR_CONFIG_CHILD)
], ...
我希望看到两个配置ERROR_CONFIG
和ERROR_CONFIG_CHILD
注入到ErrorModule 中定义的ErrorService
::>
I would like to see both configurations ERROR_CONFIG
and ERROR_CONFIG_CHILD
injected into ErrorService
defined in ErrorModule:
@Injectable
export class ErrorService {
constructor(@Inject(CONFIG) private readonly errorConfigs: ErrorConfig[])
}
但是服务的 errorConfigs
(在构造函数中)只包含 core.module
中定义的 CONFIG
- forRoot()
函数(一个元素的数组).
But the service's errorConfigs
(in constructor) contains only CONFIG
defined in core.module
- the one defined in forRoot()
function (An array with one element).
已加载和初始化延迟功能模块,并且调用了 ErrorModule.forChild(...)
当只有核心模块的 CONFIG
注入令牌可用时,ErrorService
已及时构建是有道理的 - 功能模块尚未加载.
It kind of makes sense that the ErrorService
has been constructed in time when only core module's CONFIG
injection token was available - the feature module has not beeen loaded yet.
我还尝试在构造函数之外使用 Angular Injector
导入 CONFIG
(ErrorService
中的方法),结果是一样的.
I have also tried to import the CONFIG
using Angular Injector
outside of constructor (method in ErrorService
) and the result was same.
const configs: any[] = injector.get(CONFIG); // returns only root CONFIG
所以我的问题是:是否有可能以某种方式访问 app 模块中的惰性模块提供的提供程序?(在应用的根注入器中访问它?)
So my question is: Is it possible to somehow access a provider provided by a lazy module in app module? (Access it in app's root injector?)
推荐答案
我最终得到了一个解决方案,它使用 CoreModule
中定义的 ErrorService
在 时注册配置>FeatureModule
已初始化:
I ended up with a solution, that uses ErrorService
defined in CoreModule
to register configurations when FeatureModule
is initialized:
import { ErrorService } from '@core/error.service';
@NgModule({
imports: [ CommonModule, ForFeatureModule, TranslateModule.forChild() ],
declarations: [FeatureModule],
providers: []
})
export class FeatureModule {
constructor(
private readonly translate: TranslateService, private readonly errorService: ErrorService) {
translate.setTranslation('cz', i18n, true);
// --- HERE --- errorService configures global error configuration
errorService.addErrorMappings('FEATURE1', ERROR_MAPPING);
}
}
这篇关于Angular:从惰性功能模块添加多提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!