简体   繁体   中英

adding product to header shopping cart in angular 6

we are using angular 6

I am not able to add the product to the cart's header (header components) but it's working perfectly in cart page (cart components)

I am getting data from services (cart.services). Cart components got the data but header components doesn't. why ?

`public cartItems public cartItems  :  BehaviorSubject<CartItem[]> = new 
   BehaviorSubject([]);
   public observer   :  Subscriber<{}>;
   public product    :  CartItem[] = [];
   constructor(private toastrService: ToastrService) { 
      this.cartItems.subscribe(products => this.product = products);
  }`

  `public getItems(): Observable<CartItem[]> {
    const itemsStream = new Observable(observer => {
      observer.next(this.product);
      observer.complete();
    });
    return <Observable<CartItem[]>>itemsStream;
  }`

**both component code (header and cart)**

`this.cartService.getItems().subscribe(shoppingCartItems => this.shoppingCartItems = shoppingCartItems);`

this.cartItems.subscribe(products => this.product = products);

我看不到这指向实际的服务调用以检索项目的位置

public cartItems : BehaviorSubject<CartItem[]> = new BehaviorSubject([]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM