Saltar al contenido

¿Qué es .subscribe en angular? ejemplo de código

Recuerda que en las ciencias informáticas un error casi siempere puede tener más de una soluciones, no obstante nosotros mostraremos lo más óptimo y eficiente.

Ejemplo 1: suscribirse en angular 10

let subscription = magazineStore.getMagazines().subscribe((newMagazine)=>console.log('newMagazine',newMagazine););

Ejemplo 2: suscribirse en angular 10

It's a method that comes from rxjs library which Angular is using internally.

If you can imagine yourself subscribing to a newsletter, every time there is a newnewsletter, they will send it to your home(the method inside subscribe gets called).

That's what happens when you subscribing to a source ofmagazines( which is called an Observable in rxjs library)

All the AJAX calls in Angular are using rxjs internally and in order to use anyof them, you've got to use the method name, e.g get, and then call subscribe on it, because get returns and Observable.

Also, when writing this code <button(click)="doSomething()">, Angular is using Observables internally and subscribes you to that source of event, which inthiscaseis a click event.

Back to our analogy of Observables and newsletter stores, after you've subscribed, as soon as and as long as there is a new magazine, they'll send it to you unless you go and unsubscribe from them for which you have to remember the subscription number or id, which in rxjs case it would be like :

Te mostramos las comentarios y valoraciones de los lectores

Al final de la web puedes encontrar las interpretaciones de otros sys admins, tú además tienes la libertad de mostrar el tuyo si lo deseas.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *