How to change this async pipe and take value directly?

660 views Asked by At

My compomnet.tx file had some store subscribing method like below.

readonly selectedId$: Observable<string | null> = this.store.select(selectedId)
    .pipe(shareReplayUntil(this.destroySub));

They are normally declared as variables. But I need to take this current value from them and pass it to another method in the same .ts file.

  onScanId() {
      // here I need to take subscribe id
      this.store.dispatch(scanId({ Id: "1" }));
  }

I need some expert help to resolve this. I prefer I can remove this read-only thing and assign it to directly variable, and use it in Html, the file also. Currently, I take value using an async pipe, { selectedId$ | async }

1

There are 1 answers

6
Crocsx On BEST ANSWER

What is preventing you from just doing

  this.selectedId$.pipe(take(1)).subscribe(
    Id => this.store.dispatch(scanId({ Id }))
 )

you subscribe yo your observable using take(1) to get a single value and forget about it. then just subscribe to it, get the id and dispatch