Вы не вводите компоненты в другие компоненты. Компоненты могут взаимодействовать по-разному - это не один из них.
В вашем случае я бы порекомендовал общий сервис. Добавьте сервис в два компонента, которые взаимодействуют. Сервис создает тему. Один компонент отправляет значения в тему, другой подписывается.
store.service.ts
// declare singleton service so that both components use a shared instance
@Injectable({
providedIn: 'root'
})
export class StoreService {
// use ReplaySubject if you want subscribers to receive an
// existing value on subscription
private storesFire = new ReplaySubject<any>(1);
// prefer exposing the subject via your own API rather than directly
getSelectedCoupon(): Observable<any> {
return this.storesFire.asObservable();
}
selectCoupon(coupon) {
this.storesFire.next(coupon);
}
}
pick-store.component.ts
export class PickStoreCompComponent implements OnInit {
constructor(private data: DataStorageService,
private storeService: StoreService
) {}
allCouponsData = [];
selectedCoupon;
onStoreClick() {
this.storeService.selectCoupon(this.selectedCoupon);
}
ngOnInit() {
this.allCouponsData = this.data.allCoupons;
}
}
home. component.ts
export class HomeCompComponent implements OnInit {
constructor(private storeService: StoreService) {}
private destroyed = new Subject();
ngOnInit() {
this.storeService.getSelectedCoupon.pipe(
// unsubscribe on destroy
takeUntil(this.destroyed)
).subscribe((data) => {
console.log(data);
});
}
ngOnDestroy() {
this.destroyed.next();
this.destroyed.complete();
}
}