FRANCISCO J. BLANCO
FRANCISCO J. BLANCO

Reputation: 208

Error sending data between components not related to BehaviorSubject Angular 8

I have a problem when passing data between unrelated components using Services and BehaviorSubject, the problem is when receiving the data the value of the variable Behavior arrives empty like this (""), I do not understand why I clarify my components do not have Nothing to do with each other.

My service:

@Injectable()
export class GestioOperacionesService {

    private enviaRecibe = new BehaviorSubject<string>('');
    enviaRecibe$ = this.enviaRecibe.asObservable();

    // Store message, ready to show it to whoever asks.
    enviar(mensaje) {
        // function that will call who wants to transmit a message.
        this.enviaRecibe.next(mensaje);
    }
    constructor() { }
}

My component that sends the data: (I don't upload the whole component just the method:

 Procesar(data) {
    const urlModulo = moduloAGestionar.validarModuloGestionar(data.IdModulo);
    this.gestionServiceOperacion.enviar('Envia mensaje desde componente'); // esta linea envia el mensaje
    this.router.navigate(['/Clientes/Juridicos']);
    console.log(data);
  }

My receiving component:

 CargaDataOperaciones() {
    this.gestionServiceOperacion.enviaRecibe$.pipe(take(1))
      .subscribe(mensaje => setTimeout(() => this.dataRecibida = mensaje, 0));
  }

This is the way in which I have declared the variable it receives:

public dataRecibida: string;

The latter would be the method that receives the information sent from the component and would show it in the view, but in empty arrives, I tried without the TimeOut, without the pipe without the take and nothing works.

Why is this ? What am I doing wrong or what am I missing?

Thanks for the help

Upvotes: 0

Views: 51

Answers (1)

shoban m.r
shoban m.r

Reputation: 36

I have tested your code. Go through this code

Its working fine. May be you were not calling the function CargaDataOperaciones correctly. Check it out.

Upvotes: 1

Related Questions