Angular 角度:使用replaysubject发送对象的值
我有一个add按钮,我正在执行它Angular 角度:使用replaysubject发送对象的值,angular,angular8,behaviorsubject,Angular,Angular8,Behaviorsubject,我有一个add按钮,我正在执行它 onAddProduct(productname,price){ this.purchasedProduct.push(productname); this.totalAmount += price; this.matBadgeNumber++; this.orderInfo=new OrderInfo(this.matBadgeNumber,this.purchasedProduct,this.totalAmount);
onAddProduct(productname,price){
this.purchasedProduct.push(productname);
this.totalAmount += price;
this.matBadgeNumber++;
this.orderInfo=new OrderInfo(this.matBadgeNumber,this.purchasedProduct,this.totalAmount);
this.tooltip = "you have "+ this.matBadgeNumber+ " products in cart";
}
然后我点击了执行的购买按钮
buyNow(){
this.http.orderDetailEmitter.next(this.orderInfo);
}
订单信息
export class OrderInfo {
matBadgeNumber: number;
purchasedProduct: String[];
totalAmount : Number;
constructor(matBadgeNumber: number, purchasedProduct: String[],totalAmount : Number ) {
this.matBadgeNumber = matBadgeNumber;
this.purchasedProduct = purchasedProduct;
this.totalAmount = totalAmount;
}
}
HttpClientService
readonly orderDetailEmitter = new ReplaySubject<OrderInfo>(1);
但是,当我打印orderDetails的值时,它给了我Object[Object],在使用了Oject.values(this.orderDetails)之后,它给了我一些奇怪的值
false,,false,false,false,,,,true,1,Infinity,nextInfiniteTimeWindow(value) {
const _events = this._events;
_events.push(value);
if (_events.length > this._bufferSize) {
_events.shift();
}
super.next(value);
}
我只需要通过replaysubject
http获取orderInfo对象的更改值。orderDetailEmitter
是一个可观察的对象。您需要订阅它才能获得值。还看到了OrderInfo
类,我想您是想将产品列表(purchasedProduct
属性)分配给变量this.products
,而不是Object.values(this.orderDetails)
。试试下面的方法
构造函数(私有http:HttpClientService){
this.http.orderDetailEmitter.subscribe(
详细信息=>{
this.orderDetails=详细信息;
log(“this.orderDetails”+this.orderDetails);
this.products=this.orderDetails.purchasedProduct;
console.log(“this.products”+this.products);
}
);
}
是的,这是有效的,我想以前我只使用单一值,在这种情况下,不需要订阅
false,,false,false,false,,,,true,1,Infinity,nextInfiniteTimeWindow(value) {
const _events = this._events;
_events.push(value);
if (_events.length > this._bufferSize) {
_events.shift();
}
super.next(value);
}