angular - 如何从 Action ngxs 接收 http 响应

如何在发送操作的组件中接收 http 响应。我不想将结果存储在组件中。我只想在组件中使用 http 响应。

@Action(GetNovels, { cancelUncompleted: true })
  getNovels(ctx: StateContext<Novel[]>) {
    return this.novelsService.getNovels().pipe(
      tap(novels => {
        ctx.setState(novels);
      })
    );
  }

我尝试使用像 ofActionSuccess 这样的生命周期钩子(Hook),但它只返回我发送的 Action ..

我查看了文档,但找不到有关该主题的任何特定信息。

最佳答案

您可以添加新操作 - GetNovelsComplete:

@Action(GetNovels)
getNovels(ctx: StateContext<Novel[]>) {
  return this.novelsService.getNovels().pipe(
    tap(novels => {
      ctx.setState(novels);
      ctx.dispatch(new GetNovelsComplete(novels));
    }),
    catchError(error => {
      ctx.dispatch(new GetNovelsComplete([]));
      return throwError(() => error)
    }),
  );
}

@Action(GetNovelsComplete)
getNovelsComplete() {
}

然后:

this.actions$.pipe(
  ofActionDispatched(GetNovelsComplete)
).subscribe(...);

关于angular - 如何从 Action ngxs 接收 http 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65594862/

相关文章:

php - 将动态值插入特定 WooCommerce 电子邮件模板的文本字符串中

javascript - (Javascript) 判断对象数组中是否包含一个numbervalue

tensorflow - 用 bazel 构建 tensorflow 不会产生静态库文件?

android - 如何在没有任何延迟的情况下在android中循环播放音频文件?

python - 加载 keras 模型时产生的警告

amazon-ec2 - 带 https 重定向的 EKS ingress-nginx 和 NLB

javascript - react : onChange callback not firing

jestjs - 排毒配置 - 类型 'getEnv' 上不存在属性 'typeof jasmine

python - 填充在 Mongoengine 中,引用字段

python - 我搞砸了 conda,命令在 Mac 上不起作用