Angular 组件订阅看不到调用next()的主题

Angular 组件订阅看不到调用next()的主题,angular,rxjs,Angular,Rxjs,每次我尝试订阅这个主题,它都会给我以下信息 core.js:6014错误:承诺中未捕获:NullInjectorError:StaticInjectorErrorAppModule[HomeCompComponent->PickStoreCompComponent]: StaticInjectorErrorPlatform:core[HomeCompComponent->PickStoreCompComponent]: NullInjectorError:没有PickStoreCompCompo

每次我尝试订阅这个主题,它都会给我以下信息

core.js:6014错误:承诺中未捕获:NullInjectorError:StaticInjectorErrorAppModule[HomeCompComponent->PickStoreCompComponent]: StaticInjectorErrorPlatform:core[HomeCompComponent->PickStoreCompComponent]: NullInjectorError:没有PickStoreCompComponent的提供程序! NullInjectorError:StaticInjectorErrorAppModule[HomeCompComponent->PickStoreCompComponent]: StaticInjectorErrorPlatform:core[HomeCompComponent->PickStoreCompComponent]: NullInjectorError:没有PickStoreCompComponent的提供程序

我尝试将PickStoreCompComponent添加到app.module.ts,错误消失,但订阅未看到下一个

pick-store-comp.component.ts

import { DataStorgeService } from 'src/app/data-storge.service';
import { Observable, Subject } from 'rxjs';

@Component({
  selector: 'app-pick-store-comp',
  templateUrl: './pick-store-comp.component.html',
  styleUrls: ['./pick-store-comp.component.scss']
})
export class PickStoreCompComponent implements OnInit {
  constructor(private data: DataStorgeService) {}

  storesfire = new Subject<any>();
  allCouponsData = [];
  selectedCoupon;
  onStoreClick() {
    this.storesfire.next(this.selectedCoupon);
  }

  ngOnInit() {
    this.allCouponsData = this.data.allCoupons;
  }
}
import { Component, OnInit } from '@angular/core';
import { PickStoreCompComponent } from '../pick-store-comp/pick-store-comp.component';

@Component({
  selector: 'app-home-comp',
  templateUrl: './home-comp.component.html',
  styleUrls: ['./home-comp.component.scss']
})
export class HomeCompComponent implements OnInit {
  constructor(private storesData: PickStoreCompComponent) {}

  ngOnInit() {
    this.storesData.storesfire.subscribe((data) => {
      console.log('test');
    });
  }
}


您不会将组件注入到其他组件中。组件可以以不同的方式交互——这不是其中之一

在你的情况下,我会推荐一个共享服务。将服务注入到交互的两个组件中。该服务创建一个主题。一个组件向主题发送值,另一个订阅

store.service.ts

//声明单例服务,以便两个组件都使用共享实例 @注射的{ providedIn:'根' } 导出类存储服务{ //如果希望订阅者接收请求,请使用ReplaySubject //现有认购价值 private storesFire=新ReplaySubject1; //更喜欢通过自己的API公开主题,而不是直接公开主题 GetSelected优惠券:可观察{ 返回this.storesFire.asObservable; } 选择Couponcon{ this.storesFire.nextcon; } } pick-store.component.ts

import { DataStorgeService } from 'src/app/data-storge.service';
import { Observable, Subject } from 'rxjs';

@Component({
  selector: 'app-pick-store-comp',
  templateUrl: './pick-store-comp.component.html',
  styleUrls: ['./pick-store-comp.component.scss']
})
export class PickStoreCompComponent implements OnInit {
  constructor(private data: DataStorgeService) {}

  storesfire = new Subject<any>();
  allCouponsData = [];
  selectedCoupon;
  onStoreClick() {
    this.storesfire.next(this.selectedCoupon);
  }

  ngOnInit() {
    this.allCouponsData = this.data.allCoupons;
  }
}
import { Component, OnInit } from '@angular/core';
import { PickStoreCompComponent } from '../pick-store-comp/pick-store-comp.component';

@Component({
  selector: 'app-home-comp',
  templateUrl: './home-comp.component.html',
  styleUrls: ['./home-comp.component.scss']
})
export class HomeCompComponent implements OnInit {
  constructor(private storesData: PickStoreCompComponent) {}

  ngOnInit() {
    this.storesData.storesfire.subscribe((data) => {
      console.log('test');
    });
  }
}

导出类PickStoreCompComponent实现OnInit{ 构造函数私有数据:DataStorageService, 私人商店服务:商店服务 {} allCouponsData=[]; 精选优惠券; onStoreClick{ this.storeService.selectCouponthis.selected优惠券; } 恩戈尼特{ this.allCouponsData=this.data.allCoupons; } } home.component.ts

import { DataStorgeService } from 'src/app/data-storge.service';
import { Observable, Subject } from 'rxjs';

@Component({
  selector: 'app-pick-store-comp',
  templateUrl: './pick-store-comp.component.html',
  styleUrls: ['./pick-store-comp.component.scss']
})
export class PickStoreCompComponent implements OnInit {
  constructor(private data: DataStorgeService) {}

  storesfire = new Subject<any>();
  allCouponsData = [];
  selectedCoupon;
  onStoreClick() {
    this.storesfire.next(this.selectedCoupon);
  }

  ngOnInit() {
    this.allCouponsData = this.data.allCoupons;
  }
}
import { Component, OnInit } from '@angular/core';
import { PickStoreCompComponent } from '../pick-store-comp/pick-store-comp.component';

@Component({
  selector: 'app-home-comp',
  templateUrl: './home-comp.component.html',
  styleUrls: ['./home-comp.component.scss']
})
export class HomeCompComponent implements OnInit {
  constructor(private storesData: PickStoreCompComponent) {}

  ngOnInit() {
    this.storesData.storesfire.subscribe((data) => {
      console.log('test');
    });
  }
}

导出类HomeCompComponent实现OnInit{ constructorprivate storeService:storeService{} 私人=新科目; 恩戈尼特{ this.storeService.getselected优惠券.pipe //取消订阅销毁 直到它被摧毁 .subscribedata=>{ 控制台日志数据; }; } 恩戈德斯特罗{ 这个。摧毁。下一个; 这是完全的; } }
您不会将组件的实例注入到其他组件中。您应该将主题移动到一个服务中,并将该服务注入两个组件中。非常感谢您,这是怎么回事!类型“storeService”上不存在属性“storesFire”。你是说“商店火灾”吗?