Search code examples
angularbehaviorsubject

Im trying to send str message from a componant to an other using Subject behavior "Property 'subscribe' does not exist on type '(data: any) =>void'"


I created a service to send a string from component A to componant B :

this is the service (FactureService) :


  public notificationSubject= new Subject<string>()


  constructor() {}

envoyerIdPartnerdeDialogauForm(data){
   this.notificationSubject.next(data);
  }

and this is the component A :

constructor(  private factureservice : FactureService) { }

 ngOnInit(): void {}
 
sendidPartenaire(data){
  this.factureservice.envoyerIdPartnerdeDialogauForm(data.value)
  Entre id: <input type ='text' #message />
<button   (click)="sendidPartenaire(message)">Send message</button>

and this is component B :

  idpartnerstring : string ;
constructor(){   private factureservice: FactureService}



//the problem is here in the subscribe :
//Property 'subscribe' does not exist on type '(data: any) => void'
  ngOnInit(): void {
this.factureservice.envoyerIdPartnerdeDialogauForm.subscribe(d => {

  this.idpartnerstring=d;
});

}

#i tried addind return in the service but still got the same problem


Solution

  • envoyerIdPartnerdeDialogauForm is a method in your service, subscribe to the notificationSubject instead

      ngOnInit(): void {
        this.factureservice.notificationSubject.subscribe(d => {
    
          this.idpartnerstring=d;
        });
      }