【发布时间】:2017-07-01 08:35:49
【问题描述】:
我正在尝试获取发出的最后 3 个值,下面的代码我希望在填充 uiOrder 并调用 cancelOrderItem() 几次之后,我可以使用 getHistory() 访问订单的最后 3 个修订,但是我实际上得到了最后一个(当前)值 3 次,我尝试了 replaySubject(3) 具有相同的结果。 这是我的代码:
export class OrdersService {
public readonly orders: Observable<Order[]>;
public readonly uiOrder: Observable<Order>;
private _orders: BehaviorSubject<Order[]>;
private _uiOrder: BehaviorSubject<Order>;
private dataStore: {
uiOrder: Order,
orders: Order[]
};
constructor() {
this.dataStore = {
uiOrder: null,
orders: []
};
this._orders = <BehaviorSubject<Order[]>>new BehaviorSubject([]);
this._uiOrder = <BehaviorSubject<Order>>new BehaviorSubject({});
this.orders = this._orders.asObservable();
this.uiOrder = this._uiOrder.asObservable();
}
getOrder(orderId: number | string) {
for (let i = 0; i < this.dataStore.orders.length; i++) {
if (this.dataStore.orders[i].id == orderId) {
this.dataStore.orders[i].lastAccess = moment().format().slice(0, 19) + 'Z';
this.dataStore.uiOrder = this.dataStore.orders[i];
this.updateUiOrder();
}
}
}
cancelOrderItem(action) {
this.dataStore.uiOrder.sections[action.sectionIndex].orderDetails.splice(action.orderDetailsIndex, 1);
this.updateUiOrder()
}
getHistory() {
this.uiOrder.take(3).subscribe((res) => {
console.log('uiOrder', res);
}).unsubscribe()
}
updateUiOrder() {
console.log('updating ui order');
this._uiOrder.next(this.dataStore.uiOrder);
}
}
我做错了什么?
【问题讨论】:
标签: angular typescript rxjs5