Angular/NGXS - 无法获得商店状态

在父组件上,我正在调度一个动作,我愿意在子组件上获取调度的数组,为此我正在执行以下操作:

  export class ListComponent implements OnInit {
  @Select(ProductState.getProductDetails) listProduct$: Observable<Product>;

   constructor() { 
    //Can't get the dispatched array using the following
    const list = this.listProduct$;
    console.log('The array content is ',list);
   }

  }

list$ 通常应该返回一个数组,因为我愿意对其元素运行 for 循环并过滤一些数据。

到目前为止,我无法在日志中看到数据从父组件正确分派,但我无法在子组件上获取/修改它。

lixu0394 回答:Angular/NGXS - 无法获得商店状态

如你所见,选择器从 RxJS 返回一个 Observable。 如果你想获得价值,你应该订阅它(或使用AsynPipe):

 this.listProduct$.subscribe(products => {
     // Do something with products
 });

不要忘记取消订阅,否则会导致奇怪/不可预测的错误和内存泄漏。

本文链接:https://www.f2er.com/575.html

大家都在问