Angular ngrx:访问相邻的存储片

Angular ngrx: Access to an adjacent slice of store

当我的组件初始化时,我想分派一个动作来创建“campaignDraft”对象。 如果“CampaignOverview”存在,我需要使“campaignDraft”相同,如果不存在,则将其设为预设对象 {a: 1} 例如

问题是我正在尝试使用 Effects 实现此目的,但我的所有操作都会导致浏览器冻结。

// component.ts
   ngOnInit(): void {
    this.store.dispatch(getCampaignDraft());
    this.campaignFormSubscription = this.store
          .pipe(select(selectCampaignDraft))
          .subscribe((campaignDraft: CampaignModel) => {
            this.campaignForm.patchValue(campaignDraft, { emitEvent: false });
       });
    }
          
 // actions
  export const getCampaignDraft = createAction(CampaignDraftActionTypes.GET_CAMPAIGN_DRAFT);
     
 // effects
     getCampaignDraft$ = createEffect(() => {
        return this.actions$.pipe(
          ofType(fromCampaignDraftActions.getCampaignDraft),
          switchMap((action) => withLatestFrom(this.store.pipe(select(selectCampaignOnly)))),
          map((campaign) => fromCampaignDraftActions.createCampaignDraft({ campaign: {a: 1} })) 
        );
    });

// reducer
  on(campaignDraftActions.createCampaignDraft, (state, { campaign }) => {
    return {
      ...campaign,
    };
  }),

我不知道如何检查商店是否有效 - “campaignOverview”是否存在或不使用此对象或预设对象发送另一个操作,请帮助。

我们可以使用 withLatestFrom 作为运算符而不是用 switchmap 返回它,如下所示

  getCampaignDraft$ = createEffect(() => {
    return this.actions$.pipe(
      ofType(fromCampaignDraftActions.getCampaignDraft),
      withLatestFrom(this.store.pipe(select(selectCampaignOnly))),
      map(([action, campaign]: [ActionType, CampaginStateSliceType]) => {
        if (exists) {
          return A_Action();
        } else {
          return B_Action();
        }
      })
    );
  })