NgrxStore and Angular - Use the async pipe massively or subscribe just once in the constructor

前端 未结 3 960
半阙折子戏
半阙折子戏 2020-12-04 19:06

I am starting to look at ngrx Store and I see the convenience to use the Angular async pipe. At the same time I am not sure whether using the Angular async pipe massively is

3条回答
  •  孤城傲影
    2020-12-04 19:34

    You can add ".share()" to the end of any observable declarations. All of your async pipes on an observable will then share the same subscription:

    this.name$ = Observable.create(observer => {
      console.log("Subscriber!", observer);
      return observer.next("john")
    }).delay(2000).share();
    
    this.httpget$ = http.get("https://api.github.com/").share();
    

    Plunkr demonstrating: https://embed.plnkr.co/HNuq1jUh3vyfR2IuIe4X/

提交回复
热议问题