Encountering an issue with passing data between unrelated components using Services and BehaviorSubject
. The problem arises when the data is received, as the value of the variable Behavior arrives empty (""), despite the components having no apparent connection.
Here is 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() { }
}
The component responsible for sending the data (excerpt of the method):
Procesar(data) {
const urlModulo = moduloAGestionar.validarModuloGestionar(data.IdModulo);
this.gestionServiceOperacion.enviar('Envia mensaje desde componente'); // this line sends the message
this.router.navigate(['/Clientes/Juridicos']);
console.log(data);
}
As for the receiving component:
CargaDataOperaciones() {
this.gestionServiceOperacion.enviaRecibe$.pipe(take(1))
.subscribe(mensaje => setTimeout(() => this.dataRecibida = mensaje, 0));
}
The variable that should receive the data is declared as follows:
public dataRecibida: string;
Despite setting up the method to receive the information from the component and display it in the view, the variable arrives empty. I have tried removing the TimeOut, removing the pipe, and removing the take, but nothing seems to work.
What could be causing this issue? What am I missing or doing wrong?
Thank you for any assistance provided.