【发布时间】: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