我们正在使用角度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);`
答案 0 :(得分:0)
this.cartItems.subscribe(products => this.product = products);
我看不出它指向实际的服务调用以检索项目的地方
public cartItems : BehaviorSubject<CartItem[]> = new BehaviorSubject([]);