初始化商店时未触发ROOT_EFFECTS_INIT

时间:2019-07-02 07:19:27

标签: redux ngrx

我有一个Init Effect =>

  @Effect()
  init$ = this.actions$.pipe(
    ofType(ROOT_EFFECTS_INIT),
    map(action =>
      new featureActions.GetAllMissionRequest({
        projectID: environment.projectId,
        projectContextID: environment.projectId
      })
    )
  );

我在文件底部添加了此内容,我还尝试添加了一个延迟,但是初始化我的商店时永远不会触发这种效果。

我检查了ngrx的回购,但是我申请的解决方案没有触发我的功能。

我做错什么了吗?

2 个答案:

答案 0 :(得分:1)

在这种情况下,您可以实现OnInitEffects效果:

    const userInfo$ = this.store.select(getUserInfo);
    userInfo$.pipe(
        mergeMap(info => {
            this.acntName = info.acntId;
            const departments$ = this.store.select(getAllDepts)
                .pipe(
                    map(depts => {
                        if (depts.length < 1) {
                            return this.store.dispatch(loadDeptStore({ accountId: info.acntId}));
                        } else {
                            return depts;  // get Dept data
                        }
                    })
                );
            const users$ = this.store.select(getAllUsers, { accountId: info.acntId })
                .pipe(
                    map(res => {
                        if (res.length < 1 ) {                          
                            return this.store.dispatch(loadUsersInAccountStore({billingCrmAccountId: this.acntName }));
                        } else {
                            return res; // get users data
                        }
                    })
                );
            return combineLatest([departments$, users$]); 
        }),
        map(([depts, users]) => {
            // do stuffs and return something
        })
    ).subscribe(res => console.log('results: ', res));
        
yourAction.ts

export const initEffect = createAction('[ModuleToto] init effect');

我在https://ngrx.io/guide/effects/lifecycle#oniniteffects上看到了

答案 1 :(得分:0)

要分发该事件,您必须注册效果:

@NgModule({
  imports: [
    EffectsModule.forRoot([MovieEffects])
  ],
})
export class AppModule {}

https://ngrx.io/guide/effects#registering-root-effects