Angular NG RX商店从Map()中获取对象,然后在Tap()内部效果中使​​用它

发布于 2025-02-07 03:35:20 字数 1109 浏览 2 评论 0原文

我在Angular 13项目中使用NGRX Store工作,并且效果很好。 我的问题是,我想从保存的对象中获取IF,并在Save succes后将其显示在Toastr中。

这是我的效果代码:

saveDemandeEffect: Observable<DemandeActions> = createEffect(

    () => this.effectActions.pipe(


        ofType(EnvelopeActionsTypes.SAVE_ENVELOPE),

        mergeMap((action: DemandeActions) => {

            return this.demandeService.saveDemande(action.payload)
                .pipe(
                    map((demande) => {
                        return new SaveDemandeActionSuccess(demande);
                    }),
                    tap(
                        () => {
                            // here i want to get saved demande object then demande.id to show it in the toastr msg bellow
                            this.toastr.success("message", "Confirmation")
                            this.router.navigate(['/envelopes']);
                        }
                    ),
                    catchError((err) => of(new SaveDemandeActionError(err.message)))
                )
        })
    )

);

您是否知道我该如何实现这一目标。

提前致谢

im working in angular 13 project im using ngrx store and it works fine.
my issue is that i want to get the if from my saved object and show it in the toastr after save succes.

this is my effect code :

saveDemandeEffect: Observable<DemandeActions> = createEffect(

    () => this.effectActions.pipe(


        ofType(EnvelopeActionsTypes.SAVE_ENVELOPE),

        mergeMap((action: DemandeActions) => {

            return this.demandeService.saveDemande(action.payload)
                .pipe(
                    map((demande) => {
                        return new SaveDemandeActionSuccess(demande);
                    }),
                    tap(
                        () => {
                            // here i want to get saved demande object then demande.id to show it in the toastr msg bellow
                            this.toastr.success("message", "Confirmation")
                            this.router.navigate(['/envelopes']);
                        }
                    ),
                    catchError((err) => of(new SaveDemandeActionError(err.message)))
                )
        })
    )

);

do you have any idea how i can acheive this.

Thanks in advance

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

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

发布评论

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

评论(2

孤星 2025-02-14 03:35:20

尝试卸下嵌套管道,然后使用switchmap而不是mergemap

    saveDemandeEffect: Observable<DemandeActions> = createEffect(
        () => this.effectActions.pipe(
            ofType(EnvelopeActionsTypes.SAVE_ENVELOPE),    
            switchMap((action: DemandeActions) => this.demandeService.saveDemande(action.payload)),
            tap((demande)=> {
               this.toastr.success("message Id: " + demande.id, "Confirmation")
               this.router.navigate(['/envelopes']);
            }),
            map((demande) =>new SaveDemandeActionSuccess(demande)),
            
           catchError((err) => of(new SaveDemandeActionError(err.message))
        );

Try by removing the nested pipes and use switchMap instead mergeMap.

    saveDemandeEffect: Observable<DemandeActions> = createEffect(
        () => this.effectActions.pipe(
            ofType(EnvelopeActionsTypes.SAVE_ENVELOPE),    
            switchMap((action: DemandeActions) => this.demandeService.saveDemande(action.payload)),
            tap((demande)=> {
               this.toastr.success("message Id: " + demande.id, "Confirmation")
               this.router.navigate(['/envelopes']);
            }),
            map((demande) =>new SaveDemandeActionSuccess(demande)),
            
           catchError((err) => of(new SaveDemandeActionError(err.message))
        );
他夏了夏天 2025-02-14 03:35:20

这个适合您吗?如果您在保存上的API返回需求对象,则可以在地图功能本身中采取所需的操作


saveDemandeEffect: Observable<DemandeActions> = createEffect(

    () => this.effectActions.pipe(


        ofType(EnvelopeActionsTypes.SAVE_ENVELOPE),

        mergeMap((action: DemandeActions) => {

            return this.demandeService.saveDemande(action.payload)
                .pipe(
                    map((demande) => {
                        // you can get your demande id here ?
                        this.toastr.success("message", "Confirmation")
                        this.router.navigate(['/envelopes']);
                        return new SaveDemandeActionSuccess(demande);
                    }),
                    catchError((err) => of(new SaveDemandeActionError(err.message)))
                )
        })
    )

);

Does this work for you? If you API on save returns the demande object, then you can take the required action in the map function itself


saveDemandeEffect: Observable<DemandeActions> = createEffect(

    () => this.effectActions.pipe(


        ofType(EnvelopeActionsTypes.SAVE_ENVELOPE),

        mergeMap((action: DemandeActions) => {

            return this.demandeService.saveDemande(action.payload)
                .pipe(
                    map((demande) => {
                        // you can get your demande id here ?
                        this.toastr.success("message", "Confirmation")
                        this.router.navigate(['/envelopes']);
                        return new SaveDemandeActionSuccess(demande);
                    }),
                    catchError((err) => of(new SaveDemandeActionError(err.message)))
                )
        })
    )

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