RxJs: how to “listen” for subscription changes when receiving a stream?

I am new to Angular 2 and Observables, but I have not found a way to “listen” to changes in my subscription when it receives a stream, I don’t even know if this is possible or if it is the right way.

This is what I used with promises:

// Constructor and more code
// ...

ngOnInit(): void {
  this.loading.present();

  this.getItems()
      .then(() => this.loading.dismiss());
}

getItems(): Promise {
  return this.itemService
    .getItems()
    .then(items => this.items = items);
}

refresh(refresher): void {
  this.getItems()
      .then(() => refresher.complete());
}

I tried this with subscription / observables, but I just don't know how:

// Constructor and more code
// ...

ngOnInit(): void {
  this.loading.present();

  this.getItems()
      .subscribe(() => this.loading.dismiss());
}

getItems(): Subscription {
  return this.itemService
    .getItems()
    .subscribe(items => this.items = items);
}

refresh(refresher): void {
  this.getItems()
      .subscribe(() => refresher.complete());
}

And of course, I get a compilation error:, Property 'subscribe' does not exist on type 'Subscription'any help on how to achieve this with Observables (RxJS)?

+4
source share
1 answer

A Subscriptionis a listener, you cannot listen to a listener, right?

Observable, . ( ):

getItems(): Observable<any> {
  let obs = this.itemService.getItems().share();
  obs.subscribe(items => this.items = items);
  return obs;
}
+3

Source: https://habr.com/ru/post/1654405/


All Articles