withLatestFrom()返回整个状态

时间:2019-06-21 10:20:43

标签: angular rxjs ngxs

我有以下代码:

@Select(CustomerState.customerProducts) customerProducts$: Observable<CustomerProduct[]>;

this.layouts$ = this.store.dispatch([
      new LoadCustomerProducts(),
    ]).pipe(
      withLatestFrom(this.customerProducts$),
      map(([customerProducts]) => {
        console.log('CP!!!', customerProducts);
        return this.loadLayouts(customerProducts);
      })
    );

问题是,当我登录customerProducts时,我期望它只会返回客户产品的数组,而是返回整个状态。为什么会这样?

谢谢!

1 个答案:

答案 0 :(得分:1)

$("#counter-number").html(game.countdownseconds);将值从提供的withLatestFrom中推到您要使用Observable进行解构的数组上。如果ngxs中的map返回了商店store.dispatch,则您需要:

Observable

请参阅:https://rxjs.dev/api/operators/withLatestFrom

值得注意的是,this.layouts$ = this.store.dispatch([ new LoadCustomerProducts(), ]).pipe( withLatestFrom(this.customerProducts$), map(([store, customerProducts]) => { console.log('CP!!!', customerProducts); return this.loadLayouts(customerProducts); }) ); 不会等待withLatestFrom发出。相反,当您订阅customerProducts$时,您将立即获得当前状态,但是由于layouts$尚未发出,因此customerProducts$不会发出。

https://stackblitz.com/edit/rxjs-efmtxp