【问题标题】:Update Cart Count when product add to cart in header component automatically without refresh the page in angular 6当产品自动添加到标题组件中的购物车时更新购物车计数而不刷新角度 6 中的页面
【发布时间】:2018-12-19 06:14:41
【问题描述】:

我正在开发 Angular 6 中的电子商务应用程序。我想更新标题组件中的购物车计数,当产品从购物车中添加/删除时。如果我刷新页面,两者都是不同的组件,那么只有购物车计数已更新。我尝试了共享服务在这两个组件之间共享数据并尝试了主题概念,但未能自动获取计数。如何实现?

购物车服务.ts:

async getCart():Promise<HttpResponse<Object>> {
    const cartItems = await this.httpClient.get<HttpResponse<Object>>(this.endPointService.getCartItemURL, { observe :'response',withCredentials: true })
        .toPromise();
    return cartItems;
}

header.ts:

 cartItem: CartItem[];
ngOnInit() { 

      const cartItems = <HttpResponse<Object>>await this.cartService.getCart();
      this.cartItem = <CartItem[]>cartItems.body;
}

header.html:

  <span class='cart_count notification-counter' data-toggle="modal" data-target="#exampleModal">{{cartItem.length}}</span>

尝试过的代码:

cartService.ts

 private prodCount = 0;
    prodCountCountChange: Subject<number> = new Subject<number>();
    UpdateCount(count: number) {
        this.prodCount = count;
        this.prodCountCountChange.next(this.prodCount);
    }

header.ts:

cartCount :any;
cartItem: CartItem[];
this.cartCount = this.cartService.UpdateCount(this.cartItem.length);

header.html:

  <span class='cart_count notification-counter' data-toggle="modal" data-target="#exampleModal">{{cartCount}}</span>

我还尝试了此链接中使用的行为主题 angular view is not updating after component value gets changed 没有得到所需的输出。

【问题讨论】:

  • 为什么不只使用 observables?尝试从 getCart() 返回 Observable。
  • 我无法得到你。如何从 getCart() 返回 Observable。
  • 您在那里尝试的链接也服务方法返回类型是可观察的。 Observables 和 Promises 是不同的angular.io/guide/…
  • 我要求使用promise。如果有任何方法可以使用promise。

标签: angular


【解决方案1】:

试试这个,像 Observables 应该被订阅并且 Promises 应该像这样执行。 promise

ngOnInit() { 
      this.cartService.getCart().then(
           (cartItems)=>{
                this.cartItem = <CartItem[]>cartItems.body;
         }
       );
}

【讨论】:

  • 为什么总是告诉 observable 和 promise 之间的区别。我已经知道了,但这不是问题和解决方案。
  • 然后创建stackblitz项目然后只有一个人能理解问题
  • 我没有说区别,这里我提到了promise的订阅
  • 我被清楚地告知上面查询中的问题是什么。
【解决方案2】:

您没有正确使用您的主题,并且您在购物车 service.ts 中的初始 getCart() 不会返回 Promise,而是返回结果。

[编辑]

stackblitz中可用

购物车服务.ts:

// getCard should return only datas, not an httpResponse object
// and the result should be typed
getCart(): Promise<CartItem[]> {
    return this.httpClient.get(
      this.endPointService.getCartItemURL, 
      { observe :'response',withCredentials: true }
    ).pipe(
        map((httpResponse: HttpResponse<any>) => {
            this.updateCount(httpResponse.body.length); // Don't know how your cardItem[] is formatted
            return [].concat(httpResponse.body);
        })
    ).toPromise();
}

private prodCount: number = 0;
public prodCountCountChange: Subject<number> = new Subject();
updateCount(count: number = 0): void {
    this.prodCount = count;
    this.prodCountCountChange.next(this.prodCount);
}

header.ts:

async ngOnInit() {
    this.cartItem = await this.cartService.getCart();
    this.cartService.prodCountCountChange.subscribe(
        newProdCount => this.cartCount = newProdCount
    );
}
// and don't forget to unsubscribe
ngOnDestroy() {
    this.cartService.prodCountCountChange.unsubscribe();
}

header.html:

<span class='cart_count notification-counter' data-toggle="modal" data-target="#exampleModal">{{cartCount}}</span>

希望对你有帮助。

【讨论】:

  • 未测试:我可以帮你调试吗?那我可以更新了
  • 您的购物车服务代码显示错误。您能否为您的解决方案生成 stackblitz。
  • 您在 stackblitz 中的代码也充满了错误。如果我尝试更改为
  • 好吧,我做错了,昨天没有保存最后的修改。所以我在一个新的堆栈闪电战中将它分叉并使其正确。这里是新的stackblitz
  • 感谢您的持续回复。以上代码在将产品添加到购物车时始终保留值 0
猜你喜欢
  • 1970-01-01
  • 2018-03-04
  • 2018-07-04
  • 2019-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-19
  • 1970-01-01
相关资源
最近更新 更多