关于addItem项目被push到productList数组中,但是在点击cart选项卡后,cartcomponent中的productList会被重新初始化(清空productList数组)。如何避免重新初始化数组?点击组件,成员变量是否得到重新初始化?任何其他方法来避免它?
export class CartComponent implements OnInit {
public productList: any[] = [];
constructor(private ShopDataService: ShopDataService) {
this.ShopDataService.getValue()
.subscribe(
products => {
this.productList.push(products);
console.log("cart::" + this.productList);
});
}
}
export class ShopDataService {
private products: BehaviorSubject <any[]> = new BehaviorSubject([]);
constructor() {}
public setValue(value): void {
this.products.next(value);
}
public getValue(): Observable <any[]> {
return this.products;
}
}
你可以分享更多的代码? –
点击相同的活动标签时出现问题? –
我正在将产品从另一个组件添加到购物车,并查看购物车(添加到购物车的产品列表)我必须点击购物车标签 – sue