Agular Store 效果不等待选择器数据

发布于 2025-01-16 06:46:43 字数 1640 浏览 0 评论 0原文

我正在使用 NgRx store 开发一个 Angular 项目。所有后端响应都保存在商店中。

这是我的 effects.ts

loadData$ = createEffect(() => this.actions$.pipe(
      ofType('[Data] Load Data'),
      concatMap((action: any) => of(action).pipe(
        withLatestFrom(this.store$.pipe(select(selectorData)))
        
      )),
      switchMap(([, request]: any) => this.Service.getDataApi(
          request.data1.iid,
          request.data2.iid2
        )
        .pipe(
          map((data: Data[]) => ({
              type: '[Data] Load Success',
              payload:  { data },
            }
          )),
          catchError((error: any) => of({
              type: '[Datae]] Load Failure',
              payload:  { error },
            }
          ))
        ))
    )
  );

我如何等待 selectorData 数据? 有没有具体的方法呢? 当前的实现 iddiid2 为空。

提前致谢。

编辑1 我做了以下更改。

concatMap((action: any) => of(action).pipe(
        withLatestFrom(this.store$.pipe(select(selectorData),skipWhile(iid => !iid),
        skipWhile(iid2 => !iid2),
        mapTo(true))
        )

以上更改不会使用 API 进行调用。如果数据可用,我该如何调用

编辑 2

filter(([, {configData}]: any) => configData.iid &&
      configData.iid)

我也尝试过过滤。第一次configData.iid未定义。我们如何解决这个问题

它抛出未定义的错误

编辑3 我已尝试 combineLatestWith。它不等待 this.store$.pipe(select(selectorData))

等待数据还有其他解决方案吗?

Am working on an Angular project using NgRx store. All the back end responses are saved in the store.

This is my effects.ts

loadData$ = createEffect(() => this.actions$.pipe(
      ofType('[Data] Load Data'),
      concatMap((action: any) => of(action).pipe(
        withLatestFrom(this.store$.pipe(select(selectorData)))
        
      )),
      switchMap(([, request]: any) => this.Service.getDataApi(
          request.data1.iid,
          request.data2.iid2
        )
        .pipe(
          map((data: Data[]) => ({
              type: '[Data] Load Success',
              payload:  { data },
            }
          )),
          catchError((error: any) => of({
              type: '[Datae]] Load Failure',
              payload:  { error },
            }
          ))
        ))
    )
  );

How can i wait for selectorData data?
Is there any specific method for that?
Current implementation idd and iid2 are empty.

Thanks in advance.

Edit 1
I have made below changes

concatMap((action: any) => of(action).pipe(
        withLatestFrom(this.store$.pipe(select(selectorData),skipWhile(iid => !iid),
        skipWhile(iid2 => !iid2),
        mapTo(true))
        )

Above changes it will not call with the API.how can i call if the data is available

Edit 2

filter(([, {configData}]: any) => configData.iid &&
      configData.iid)

I have tried filter as well. First time configData.iid is undefined.how can we solve this issue

It throwing undefined error

Edit 3
I have tried combineLatestWith.it doesn't wait for this.store$.pipe(select(selectorData))

Any other solution for waiting data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素衣风尘叹 2025-01-23 06:46:43

您可以使用combineLatest来等待多个操作或源可观察量。

loadData $ = createEffect(() => {
    return combineLatest(
      this.actions$.pipe(
          ofType('[Data] Load Data')
      ),
      this.store$.pipe(
          select(selectorData),
          filter(iid => !!iid) // Filters untruthy values
      )
    ).pipe(
      // Wont come here until both observables have emitted
      ...
    )

You can use combineLatest to wait for multiple actions or source observables.

loadData $ = createEffect(() => {
    return combineLatest(
      this.actions$.pipe(
          ofType('[Data] Load Data')
      ),
      this.store$.pipe(
          select(selectorData),
          filter(iid => !!iid) // Filters untruthy values
      )
    ).pipe(
      // Wont come here until both observables have emitted
      ...
    )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文