NGXS-如何在多个流上使用一个动作处理程序

Noe*_*out 0 ngxs

我有几个异步运行的动作,比如说VerifyEmailchangepassword。我想在动作开始时显示加载程序,并在拍卖结束时隐藏它。我知道我可以通过以下方式完成每个操作:

this.actions$
    .pipe( ofActionDispatched(VerifyEmail) )
    .subscribe( () => {
        this.showLoader();
    });
this.actions$
    .pipe( ofActionSuccessful(VerifyEmail) )
    .subscribe( () => {
        this.hideLoader();
    });
Run Code Online (Sandbox Code Playgroud)

但是每个异步动作有两个块,我想知道是否有一种方法可以将多个动作组合到一个管道中?像ofActionDispatched([VerifyEmail, ChangePassword])什么?

Mar*_*eld 6

您可以将运算符组合为多个操作,如下所示:

ofActionDispatched(VerifyEmail, ChangePassword)
Run Code Online (Sandbox Code Playgroud)

请注意,它只是方法的附加参数,而不是数组。

作为替代方案,您可以将此加载器绑定loading到处于您状态的某个位置的道具@Action,并在进行异步工作之前和之后从各自的处理程序中更新该道具:

@Action(VerifyEmail)
async verifyEmail(ctx, action){
    ctx.patchState({ loading: true });
    await someasyncWork();
    ctx.patchState({ loading: false });
}
Run Code Online (Sandbox Code Playgroud)

还有一种选择是在调度之前和完成时从UI组件调用此方法:

this.showloader();
this.store.dispatch(new VerifyEmail())
    .subscribe(() => this.hideLoader());
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。