我怎样才能从商店获得两个影响变量,用 angular ngrx 将它发送到 API

How can I get two variables in effects from store to send it to API with angular ngrx

这是效果方法 我尝试获取跳过号码和 select 用户将其发送到 Api,跳过没问题,但 select 用户不行..

loadMoreAction$ = createEffect(() =>
    this.actions$.pipe(
      ofType(directMessagesAction.loadMoreAction),
      withLatestFrom(
        this.store.pipe(select(selectMyConversation)),
        this.store.pipe(select(selectUser))
      ),
      map(([{}, skip]) => skip),
      switchMap((skip) =>
        this.signalRService
          .getMoreMessageHistory({ skip })
          .pipe(
            map((result) =>
              directMessagesAction.loadMoreActionFinished({ payload: result })
            )
          )
      )
    )
  );

**这是应用组件**

  onUp(ev: any) {
    this.store.dispatch(directMessagesAction.loadMoreAction());
    this.spinner.show();
    console.log("scrolled up!", ev);
  }

这是select应用组件中的在线和离线用户

 selectChat(onlineusermodel: OnlineUserModel): void {
    console.log("DMC: selectedOnlineUserName   :" + onlineusermodel.userName);
    this.selectedOnlineUserName = onlineusermodel;
    this.stateUser = true;
    this.store.dispatch(
      directMessagesAction.selectedOnlineUserName({
        payload: this.selectedOnlineUserName,
      })
    );
  }

  selectChatOffline(offlineUserModel: OfflineUserModel): void {
    console.log("DMC: selectedOfflineUserName   :" + offlineUserModel.userName);
    this.selectedOnlineUserName = offlineUserModel;
    this.stateUser = false;
    this.store.dispatch(
      directMessagesAction.selectedOnlineUserName({
        payload: this.selectedOnlineUserName,
      })
    );
  }

操作:

export const selectedOnlineUserName = createAction(
  "[DM] LOAD SELECTED ONLINE OFFLINE USER",
  props<{ payload: OnlineUserModel }>()
);

要解决的问题是在 selected 用户上获得无限滚动功能,以通过跳过和用户名

获取历史记录

由于您正在使用 ofType NgRX operator, with the withLatestFrom 运算符并将两个 Observable(s) 传递给它,因此 map 运算符的输入在这种情况下将是一个数组3 个变量为 [moreActionParam, skip, user],而不是 [{}, skip]

您可以尝试以下方法:

loadMoreAction$ = createEffect(() =>
    this.actions$.pipe(
        ofType(directMessagesAction.loadMoreAction),
        withLatestFrom(
            this.store.pipe(select(selectMyConversation)),
            this.store.pipe(select(selectUser))
        ),
        switchMap(([moreActionParam, skip, user]) =>
            // You can use `user` here...
            this.signalRService
                .getMoreMessageHistory({ skip })
                .pipe(map((result) => directMessagesAction.loadMoreActionFinished({ payload: result })))
        )
    )
);