8
votes

I am trying to use share() with subscribe() but got error message as follow. Initially, i started with subscribe. How can this be fix?

My intention is to execute the logic in subscribe. Share is needed to prevent multiple call with async pipe.

Type 'Subscription' is not assignable to type 'Observable'.
Property '_isScalar' is missing in type 'Subscription'. (property) PostDetailPage.post: Observable

 this.post = this.data.getPostById(this.postId).share().subscribe(data => {
          this.post = data;
      },
      err => {
          console.log("Oops!");
      })
1

1 Answers

16
votes

.subscribe() returns a Subscription (allows to unsubscribe).
If you want an Observable, don't use subscribe(...).
You can instead use map(...)

  this.post = this.data.getPostById(this.postId).share().map(data => {
      this.post = data;
  })