Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  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 :

In your component B, you are trying to subscribe to a method called envoyerIdPartnerdeDialogauForm which of course will not work because it is not supposed to be subscribed to it.

I suggest the following:

In your service, add the following method, which returns you an observable from the subject that you created:

notificationSubject$(): Observable<string> {
   return this.notificationSubject.asObservable();
}

Then you can subscribe to this method in your component B:

ngOnInit(): void {
   this.factureservice.notificationSubject$().subscribe(value => {
      this.idpartnerstring = value;
   });
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading