【问题标题】:adding product to header shopping cart in angular 6以角度 6 将产品添加到标题购物车
【发布时间】:2019-04-28 06:59:42
【问题描述】:

我们正在使用角度 6

我无法将产品添加到购物车的标题(标题组件),但它在购物车页面(购物车组件)中运行良好

我正在从服务 (cart.services) 获取数据。购物车组件获得了数据,但标题组件没有。为什么?

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

【问题讨论】:

    标签: javascript angular angular-components


    【解决方案1】:

    this.cartItems.subscribe(products =&gt; this.product = products);

    我没有看到这指向了一个实际的服务调用来检索项目

    public cartItems : BehaviorSubject&lt;CartItem[]&gt; = new BehaviorSubject([]);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-09
      • 1970-01-01
      • 2011-12-19
      • 2014-11-21
      • 2019-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多