首页 > 解决方案 > 我在页眉上显示的行为主题没有刷新

问题描述

在我的电子商务的标题上,我显示了我的购物车元素的行为主题计数器,每次点击添加到购物车后都会更新这个计数器,但它不显示新值,是因为标题加载了一个时间?

标头 ngOnInit 函数:

ngOnInit() { 
  this.itemCount = this.shoppingCartService.countBS;
  ...
}

标题 Html:{{itemCount | 异步}}

购物车服务:

export class ShoppingCartService {
    ....
    public countBS: BehaviorSubject<number>;

public constructor() {
    ....
    this.countBS =  new BehaviorSubject<number>(0);
}

添加物品功能:

private SetCount(quantity: number): void {
    this.count += quantity;
    this.countBS.next(this.count);
    this.storage.setItem('countCart', this.count.toString());
}

谢谢您的帮助

标签: angulartypescriptrxjs

解决方案


推荐阅读