[英]Injecting dependencies inside the NgModule import section of an angular 2 app
I need to be able to inject and use a angular 2 service (called SomeService
below) from the imports section of NgModule
:我需要能够从
NgModule
的导入部分注入和使用 angular 2 服务(下面称为SomeService
):
...
imports: [
BrowserModule,
RouterModule.forRoot(AppRoutes, {useHash: true}),
HttpModule,
ReactiveFormsModule,
NgbModule,
StoreModule.provideStore({
currentUserAccount: someFunction(currentUserAccountReducer, someService),
authenticated: someFunction(authenticatedReducer, someService)
},
{
authenticated: false
})
],
...
I need that because I need to use a fully functional service (that depends on Http
) from a plain function (named someFunction
above) in order to rehydrate a ngrx store application.我需要它,因为我需要使用来自普通函数(上面命名为
someFunction
)的全功能服务(取决于Http
),以便为ngrx 商店应用程序补水。
Here someFunction
is a meta reducer.这里的
someFunction
是一个元减速器。
See concept of meta reducer in ngrx store application.请参阅 ngrx 商店应用程序中元减速器的概念。
Can someone please help?有人可以帮忙吗?
Not completely sure I understand what you are trying to do (without seeing more code), but I imagine what you are trying to do can be done by using a factory for the provider configuration不完全确定我了解您要做什么(没有看到更多代码),但我想您可以通过使用工厂进行提供程序配置来完成您要做什么
providers: [
{
provide: WhateverService,
useFactory: (things: Things, to: To, inject: Inject) => {
// Not 100% sure, but I believe the return should be
// synchronous. If you have some asynchronous actions
// to be resolved first, you may just want to pass the
// Promise or Observable to the constructor
return new WhateverService(...);
},
deps: [Things, To, Inject]
}
]
In your StoreModule
, it could be something like在您的
StoreModule
,它可能类似于
@NgModule({})
export class StoreModule {
static provideStore(variable) {
return {
ngModule: StoreModule,
providers: [
{
provide: WhateverService,
useFactory: (things: Things, to: To, inject: Inject) => {
// use variable here
return new WhateverService(...);
},
deps: [Things, To, Inject]
}
]
}
}
}
Another option, if you are trying to resolve some remote data before bootstrap, is to do something like this .另一种选择是,如果您尝试在引导之前解析某些远程数据,则执行以下操作。 Other than that, I may be completely off, as I am not familiar with ngrx.
除此之外,我可能完全不知道,因为我不熟悉 ngrx。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.