无法从效果中调用动作

发布于 2025-01-17 18:16:40 字数 1249 浏览 0 评论 0原文

Am New in Angular和Ngrx商店概念(NGRX和Angular版本为13)。

我正在尝试调用操作表格我的createefect in fefferess.ts files.ts file

createeefect方法在这里像这样

loadData$ = createEffect(() => this.actions$.pipe(
      ofType('[Data] Load data'),
      mergeMap(() => this.myService.getDataApi()
        .pipe(
          map((data: Data[]) => ({      
            type: '[Data] Load Success',
            payload:  { data},
            selectedData:data[0],

            return setData(selectedData),
            }
          )),
          catchError((error: any) => of({
            type: '[Data] Load Failure',
            payload:  { error },
            }
          ))
        ))
    )
  );

在调用一个api,称为getDataapi。一旦我从后端收到数据后,设置Payod并键入type.finally Am调用setData,然后从响应中传递拳头值。

setData Action

export const setData = createAction(
    '[Data] Set Selected',
      props<{  selectedData: Data}>(),
  );

错误

“在此处输入图像说明”

预先感谢。

Am new in angular and ngrx store concept(ngrx and angular version is 13).

am trying to call an action form my createEffect method in effects.ts file

createEffect method look like this

loadData$ = createEffect(() => this.actions$.pipe(
      ofType('[Data] Load data'),
      mergeMap(() => this.myService.getDataApi()
        .pipe(
          map((data: Data[]) => ({      
            type: '[Data] Load Success',
            payload:  { data},
            selectedData:data[0],

            return setData(selectedData),
            }
          )),
          catchError((error: any) => of({
            type: '[Data] Load Failure',
            payload:  { error },
            }
          ))
        ))
    )
  );

Here am calling an API called getDataApi. Once i received the data from back end,setting payod and type.Finally am calling setDataand pass the fist value from response.

SetData action

export const setData = createAction(
    '[Data] Set Selected',
      props<{  selectedData: Data}>(),
  );

error

enter image description here

Thanks in advance.

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

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

发布评论

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

评论(1

自由如风 2025-01-24 18:16:40

你的代码怎么了?这显然是错误的部分:

map((data: Data[]) => ({      
            type: '[Data] Load Success',
            payload:  { data},
            selectedData:data[0],

            return setData(selectedData),
            }
          )),

更改为:

switchMap((data: Data[]) => setData({ selectedData: data[0] })),

What's up with your code? This is a wrong part obviously:

map((data: Data[]) => ({      
            type: '[Data] Load Success',
            payload:  { data},
            selectedData:data[0],

            return setData(selectedData),
            }
          )),

Change with:

switchMap((data: Data[]) => setData({ selectedData: data[0] })),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文