CombineLatest returns 错误值
CombineLatest returns wrong value
我意识到订阅 returns 一个错误的值
// in ts file of a component
combineLatest([this.editSrv.getOrders(), this.route.queryParams]).subscribe(data => {
this.orders = data[0];
...
})
和
// in editSrv
getOrders(){
// orders$: BehaviourSubject
if(this.orders$.getValue().length > 0){
// ordersSource = this.orders$.asObservable()
return this.ordersSource$;
}
return this.http.get<any[]>("retrieve orders from api").pipe(
map(orders => {
this.orders$.next(orders);
return orders;
})
)
}
当我现在删除组件中具有以下功能的订单时
removeOrder(order){
let foundOrder = this.orders.find(o => o.id == order.id);
if(foundOrder){
const index = this.orders.indexOf(foundOrder);
this.orders.splice(index,1);
}
}
并用这个函数改变当前路线的queryParams
cancel(){
this.router.navigate([]);
}
上面的 combineLatest
被触发但是 this.editSrv.getOrders()
returns orders
没有删除订单虽然我没有调用 next
但是为什么 getOrders()
returns 现在除了我在组件中删除的订单之外的所有订单都没有调用 next
?不应该保持原样吗?
我认为你需要做 this.orders$.next(foundOrder);删除项目后;
if(foundOrder){
const index = this.orders.indexOf(foundOrder);
this.orders.splice(index,1);
this.orders$.next(foundOrder);
}
然后我相信您将不再需要 combineLatest
我意识到订阅 returns 一个错误的值
// in ts file of a component
combineLatest([this.editSrv.getOrders(), this.route.queryParams]).subscribe(data => {
this.orders = data[0];
...
})
和
// in editSrv
getOrders(){
// orders$: BehaviourSubject
if(this.orders$.getValue().length > 0){
// ordersSource = this.orders$.asObservable()
return this.ordersSource$;
}
return this.http.get<any[]>("retrieve orders from api").pipe(
map(orders => {
this.orders$.next(orders);
return orders;
})
)
}
当我现在删除组件中具有以下功能的订单时
removeOrder(order){
let foundOrder = this.orders.find(o => o.id == order.id);
if(foundOrder){
const index = this.orders.indexOf(foundOrder);
this.orders.splice(index,1);
}
}
并用这个函数改变当前路线的queryParams
cancel(){
this.router.navigate([]);
}
上面的 combineLatest
被触发但是 this.editSrv.getOrders()
returns orders
没有删除订单虽然我没有调用 next
但是为什么 getOrders()
returns 现在除了我在组件中删除的订单之外的所有订单都没有调用 next
?不应该保持原样吗?
我认为你需要做 this.orders$.next(foundOrder);删除项目后;
if(foundOrder){
const index = this.orders.indexOf(foundOrder);
this.orders.splice(index,1);
this.orders$.next(foundOrder);
}
然后我相信您将不再需要 combineLatest