NGXS dispatch-decorator 插件不适用于 withLatestFromOperator

NGXS dispatch-decorator plugin not working with withLatestFromOperator

我使用 NGXS 作为我的状态管理及其插件 https://github.com/ngxs-labs/dispatch-decorator

我的组件上有以下代码:

this.route.paramMap.pipe(
  tap((params: ParamMap) => {
    this.facade.initializeForm();
  }),
  switchMap((params: ParamMap) => {
    const id = +params.get('id');
    return of(this.facade.viewOrder(id));
  }),
  withLatestFrom(this.facade.currentlyViewedDelivery$),

).subscribe((delivery) => {
  console.log('DELIVERY', delivery);
});

这是我的 viewOrder():

  @Dispatch() viewOrder = (id: number) => new OrderAction.View(id);

但是,我订阅的结果告诉我 currentlyViewedDelivery$ returns 的值为 null。当我在 withLatestFrom() 之前尝试 delay() 运算符时,它起作用了。它让我知道调度操作尚未完成。但是,当我尝试使用 Store 进行正常调度时,它可以在不使用 delay() 的情况下工作。可能是什么问题?

谢谢!

我不确定你想要达到什么目的,但是 @Dispatch 装饰器不应该那样使用。

这个插件有什么作用?基本上它会覆盖你的方法,获取它的 return 值(它必须是一个单一的动作,ObservablePromise),打开它(订阅或解析 Promise)和在后台为您调用 dispatch

我假设 View 操作处理程序执行一些异步作业,这就是为什么您在使用 delay 运算符进行管道传输时看到更新的状态值。

坦率地说,你必须要听 View 动作完成,看下面的代码:

this.route.paramMap
  .pipe(
    tap((params: ParamMap) => {
      this.facade.initializeForm();
    }),
    switchMap((params: ParamMap) => {
      const id = +params.get('id');
      this.facade.viewOrder(id);
      return this.actions$.pipe(ofActionSuccessful(OrderAction.View), take(1));
    }),
    withLatestFrom(this.facade.currentlyViewedDelivery$)
  )
  .subscribe(delivery => {
    console.log('DELIVERY', delivery);
  });

其中 actions$ 是注入的 Actions class.

如果您更喜欢使用外观,那么您可以将此逻辑移动到某个外观方法并调用其他方法:)