Problem of connecting an Observable

Asked

Viewed 19 times

0

First of all, excuse my Portuguese, I don’t write it well and I’m using a translator, but I can read it well enough.

I have a problem connecting an observable that I create in a service with a variable of a component. Here is the code:

servicio1.ts

         private myObservable$ = new Subject<myInterface>();
            constructor() { 
              this.crearObservable$();
            }

         nuevoObjeto(dato1: string, dato2: string): myInterface {
              return {
              datoUno: dato1,
              datoDos: dato2,
             };
         }
    
         crearObservable$() {
              let observableAUX: myInterface;
              observableAUX = this.nuevoUsuario("Dato 1", "Dato 2");
              this.myObservable$.next(observableAUX);
         }

         getObservable$(): Observable<myInterface> {
              return this.myObservable$.asObservable();
         }

Component1.ts

         datos: Observable<myInterface>;
         datos: Usuario;

         ngOnInit(): void {
            this.datos$ = this.servicio1.getObservable$();
            this.datos$.subscribe(datos => this.datos = datos);
            console.log(this.datos);
         } 

Of course both the service and the interface are not imported anywhere.

But for some reason `these.data' do not receive the values.

I’d appreciate your help.

Thank you in advance.

1 answer

0


The problem was that I had to sign up before transmitting, so I only had to call createObservable$() after I signed up:

ngOnInit(): void {
    this.datos$ = this.servicio1.getObservable$();
    this.datos$.subscribe(datos => this.datos = datos);
    console.log(this.datos);
    this.servicio1.crearObservable$();
} 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.