Angular withLatestFrom()返回整个状态
我有以下代码:Angular withLatestFrom()返回整个状态,angular,rxjs,ngxs,Angular,Rxjs,Ngxs,我有以下代码: @Select(CustomerState.customerProducts) customerProducts$: Observable<CustomerProduct[]>; this.layouts$ = this.store.dispatch([ new LoadCustomerProducts(), ]).pipe( withLatestFrom(this.customerProducts$), map(([cu
@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);
})
);
@Select(CustomerState.customerProducts)customerProducts$:可观察;
this.layouts$=this.store.dispatch([
新建LoadCustomerProducts(),
]).烟斗(
使用最新自(this.customerProducts$),
映射([customerProducts])=>{
log('CP!!!',customerProducts);
返回此.loadLayouts(customerProducts);
})
);
问题是,当我记录customerProducts时,我希望它只返回CustomerProduct数组,而返回整个状态。为什么会这样
谢谢
withLatestFrom
将所提供的可观察值
中的值推送到使用map
解构的数组中。如果ngxs中的store.dispatch
返回可观察的存储,则需要:
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$
尚未发出,layout$
将不会发出
它无法知道您将通过LoadCustomerProducts
操作更改状态的哪一部分。顺便说一句,.dispatch
似乎没有返回任何@martin的信息,这是我正在使用的ngxs的ngrx源代码。顺便说一句,已经有答案了。谢谢你!啊啊,我的错!