Angular6 角度6-使用服务在组件之间发送对象

Angular6 角度6-使用服务在组件之间发送对象,angular6,Angular6,事实上,我遇到了两个问题。 我的第一个任务是在不同组件的篮子中添加文章。我向服务发送一个对象,然后由第二个组件检索它 我的第二个问题是,当我想要第二个产品时,我希望同时显示这两个产品,而不是覆盖它。我一直在读:为了给我一些技能 在我的视图中,我添加了一个按钮: <button class="btn btn-danger" (click)="onAddProduct()">Ajouter</button> export class ProductComponent imp

事实上,我遇到了两个问题。 我的第一个任务是在不同组件的篮子中添加文章。我向服务发送一个对象,然后由第二个组件检索它

我的第二个问题是,当我想要第二个产品时,我希望同时显示这两个产品,而不是覆盖它。我一直在读:为了给我一些技能

在我的视图中,我添加了一个按钮:

<button class="btn btn-danger" (click)="onAddProduct()">Ajouter</button>
export class ProductComponent implements OnInit {
   @Input() private product: Product;
   private basketData: BasketdataService;

   constructor(private basketData: BasketdataService) {  }

   ngOnInit() {
   }
   onAddProduct() {
     console.log(this.product);
     this.basketData.onAddBasket(this.product);
   }
}
我的服务在这里检索数据并与basket组件共享它们

export class BasketdataService {
  private subject = new Subject<any>();

  onAddBasket(product: Product) {
    this.subject.next(product);
  }

  onGetBasket(): Observable<Product> {
    return this.subject.asObservable();

  }
  removeArticleBasket() {
    this.subject.next();
  }
我收到这个错误:
错误引用错误:未在SafeSubscriber上定义产品。\u接下来在basketComponent中出现错误,请尝试以下操作:

export class BasketmenuComponent implements OnInit, OnDestroy {
  product : Product;
  subscription : Subscription;

  constructor(private basketdataService: BasketdataService) {
    this.subscription = this.basketdataService.onGetBasket().subscribe(product=> { this.product = product; });
  }
  ngOnInit() {
  }
  ngOnDestroy() {
    // unsubscribe to ensure no memory leaks
    this.subscription.unsubscribe();
  }
}

在这个订阅中不是“消息”,而是“产品”

export class BasketmenuComponent implements OnInit, OnDestroy {
  product : Product;
  subscription : Subscription;

  constructor(private basketdataService: BasketdataService) {
    this.subscription = this.basketdataService.onGetBasket().subscribe(product=> { this.product = product; });
  }
  ngOnInit() {
  }
  ngOnDestroy() {
    // unsubscribe to ensure no memory leaks
    this.subscription.unsubscribe();
  }