我正面临以下问题。在我的减速器初始化之前,我的动作已被触发。因此,我的减速器不采取任何措施。在Reducer初始化之前被解雇后,Reducer会采取行动,对我有什么影响?
此图为您展示了redux流。
我的操作是从ConfigurationEffect调度的。并且应该由不同的XFeature减少器捕获,这些减少器是基于功能的减少器
代码示例:
应用模块
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
CommonModule,
AppRoutingModule,
HttpClientModule,
StoreModule.forFeature('config', fromConfig.reducer),
EffectsModule.forFeature([ConfigEffects]),
EffectsModule.forRoot([AppEffects]),
StoreModule.forRoot(reducers, { metaReducers }),
StoreDevtoolsModule.instrument({
maxAge: 10,
name: 'Debug DevTools',
logOnly: true,
actionSanitizer,
stateSanitizer,
}),
],
bootstrap: [AppComponent],
})
export class AppModule {}
配置效果
@Effect()
loadConfigs$ = this.actions$.pipe(
ofType(configActions.ConfigActionTypes.LoadConfigs),
switchMap((action: configActions.LoadConfigs) =>
this.configService.loadConfig().pipe(
map((result: ConfigResponse) => new configActions.SaveConfigs(result)),
catchError((err: HttpErrorResponse) => of(new configActions.FailConfigs(err.message))),
),
),
);
xFeature reducer
export function reducer(state = initialState, action: XFeatureActions | ConfigActions): XFeatureState {
switch (action.type) {
// save configs, this will be called when the device is initially loaded.
case ConfigActionTypes.SaveConfigs:
...
})
答案 0 :(得分:0)
我不确定这是否是您要寻找的东西,但是当注册一个reducer时,NgRx会调度一个update-reducers
动作。您可以听取效果内部的这些动作,以使SaveConfig
动作动摇。
NgRx 7之前的版本如下:
{type: '@ngrx/store/update-reducers', feature: 'feature1'}
{type: '@ngrx/store/update-reducers', feature: 'feature2'}
从NgRx 7开始,其外观如下:
{type: '@ngrx/store/update-reducers', features: ['feature1',
'feature2']}
答案 1 :(得分:0)
this.store.dispatch(new Action());
this.router.navigate(['/lazy']);
但是过了一会儿,我得到了它应该以这种方式工作,以等待延迟模块加载(这意味着注册所有功能,例如reducer,effects ...)以执行操作。
this.router.navigate(['/lazy'])
.then(_ => {
this.store.dispatch(new Action());
});