Home > Mobile >  Im trying to send str message from a componant to an other using Subject behavior "Property �
Im trying to send str message from a componant to an other using Subject behavior "Property �

Time:06-01

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

CodePudding user response:

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;
   });
}

CodePudding user response:

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

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

      this.idpartnerstring=d;
    });
  }
  • Related