Angular: 使用replaysubject发送对象的值

Angular: Sending the value of object using replaysubject

我有一个添加按钮,我正在其上执行

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);

OrderDetailsComponent

export class OrderDetailsComponent implements OnInit {

  products;
  orderDetails;

  constructor(private http : HttpClientService) {

    this.orderDetails=this.http.orderDetailEmitter;
    console.log("this.orderDetails"+this.orderDetails);
    this.products = Object.values(this.orderDetails);
    console.log("this.products"+this.products);


   }

  ngOnInit(): void {

  }

}

但是当我打印 orderDetails 的值时,它给了我对象 [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

获取 orderInfo 对象的更改值

http.orderDetailEmitter 是可观察的。您需要订阅它以获取值。还看到 OrderInfo class,我想你的意思是将产品列表 (purchasedProduct 属性) 分配给变量 this.products 而不是 Object.values(this.orderDetails) .尝试以下

constructor(private http : HttpClientService) {
  this.http.orderDetailEmitter.subscribe(
    details => {
      this.orderDetails = details;
      console.log("this.orderDetails"+this.orderDetails);
      this.products = this.orderDetails.purchasedProduct;
      console.log("this.products"+this.products);
    }
  );
}