Angular NGRX调度第二动作与第一个动作的数据

发布于 2025-02-12 09:12:50 字数 798 浏览 1 评论 0原文

我在Angular 13项目中工作,我使用NGRX商店,我的效果以下效果很好:

searchEnterpriseEffect: Observable<Action> = createEffect(

    () => this.effectActions.pipe(

        ofType(EnterpriseActionsTypes.SEARCH_ENTERPRISE),

        switchMap((action: EnterpriseActions) => {

            return this.enterpriseService.getEnterpriseById(action.payload)
                .pipe(
                    map((enterprise) => new SearchEnterpriseActionSuccess(enterprise)),

                    catchError((err) => of(new SearchEnterpriseActionError(err.status)))
                )
        })
    )
);

我的问题是我想在searchenterPriseActionseactionsuccess usched Chinish之后获取企业对象ID,然后派遣一个新的商店操作,称为SearchDemandedata(EntrpriseId),该操作采用该操作来自先前动作返回的状态的数据。

您有任何想法,请我如何正确地做到这一点。

问候。

im working in Angular 13 project and i use ngRx store, i have the following effects that works fine :

searchEnterpriseEffect: Observable<Action> = createEffect(

    () => this.effectActions.pipe(

        ofType(EnterpriseActionsTypes.SEARCH_ENTERPRISE),

        switchMap((action: EnterpriseActions) => {

            return this.enterpriseService.getEnterpriseById(action.payload)
                .pipe(
                    map((enterprise) => new SearchEnterpriseActionSuccess(enterprise)),

                    catchError((err) => of(new SearchEnterpriseActionError(err.status)))
                )
        })
    )
);

my issue is that i want to get the enterprise object Id after SearchEnterpriseActionSuccess finish then dispatch a new store action called SearchDemandeData(enterpriseId) that takes the data from state returned by previous action.

do you have any idea please how i can do this correctly.

Regards.

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

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

发布评论

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

评论(3

幻想少年梦 2025-02-19 09:12:51

而不是返回地图,而是返回SwitchMap。现在,它将期望和可观察到的数组,并且在数组内您可以根据需要派遣尽可能多的动作。

switchMap((enterprise) => [
  new SearchEnterpriseActionSuccess(enterprise),
  new SearchDemandeData(enterpriseId)
]);

Instead of returning a map, returns a switchMap. Now it will expects either and Observable or an Array, and inside the array you can dispatch as many actions as you want.

switchMap((enterprise) => [
  new SearchEnterpriseActionSuccess(enterprise),
  new SearchDemandeData(enterpriseId)
]);
二手情话 2025-02-19 09:12:51

将属性传递给下一个动作:

searchEnterpriseEffect: Observable<Action> = createEffect(

() => this.effectActions.pipe(

ofType(EnterpriseActionsTypes.SEARCH_ENTERPRISE),

switchMap((action: EnterpriseActions) => {

return this.enterpriseService.getEnterpriseById(action.payload)
.pipe(
// grab the action's payload and add it to the next action

Pass along the properties to the next action:

searchEnterpriseEffect: Observable<Action> = createEffect(

    () => this.effectActions.pipe(

        ofType(EnterpriseActionsTypes.SEARCH_ENTERPRISE),

        switchMap((action: EnterpriseActions) => {

            return this.enterpriseService.getEnterpriseById(action.payload)
                .pipe(
//                           grab the action's payload and add it to the next action ????
                    map((enterprise) => new SearchEnterpriseActionSuccess(enterprise, action.payload)),

                    catchError((err) => of(new SearchEnterpriseActionError(err.status)))
                )
        })
    )
);
┈┾☆殇 2025-02-19 09:12:50

您应该创建其他效果,以searchenterPriseActionsuccess,从企业中提取ID,并使用提取的ID派遣新操作searchDemandEdata

我不知道您的确切实现,但这应该与以下内容相似。

searchDemandeOnEnterpriseSuccessEffect: Observable<Action> = createEffect(

  () => this.effectActions.pipe(

    // filter by SearchEnterpriseActionSuccess
    ofType(EnterpriseAPIActionsTypes.SEARCH_ENTERPRISE_SUCCESS),

    // map the action to SearchDemandeData Action using the enterprise id
    map((action: SearchEnterpriseActionSuccess) => 
      (new SearchDemandeData(action.enterprise.id))
    )

  )

);

干杯

You should create other effect that listens to SearchEnterpriseActionSuccess, extract the id from the enterprise and dispatch the new action SearchDemandeData with the extracted id.

I don't know your exact implementation, but it should be something similar to the following.

searchDemandeOnEnterpriseSuccessEffect: Observable<Action> = createEffect(

  () => this.effectActions.pipe(

    // filter by SearchEnterpriseActionSuccess
    ofType(EnterpriseAPIActionsTypes.SEARCH_ENTERPRISE_SUCCESS),

    // map the action to SearchDemandeData Action using the enterprise id
    map((action: SearchEnterpriseActionSuccess) => 
      (new SearchDemandeData(action.enterprise.id))
    )

  )

);

Cheers

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