0

I have a PollDetailResolve class that resolves a poll before being navigating to the poll-detail page. However, I also need to resolve a separate piece of data (a boolean that we'll call canVote) that will also make use of PollService.

What is the best way to resolve two or more pieces of data?

@Injectable()
export class PollDetailResolve implements Resolve<Poll> {
  constructor(private pollService: PollService, private router: Router) { }

  resolve(route: ActivatedRouteSnapshot): Observable<Poll> | boolean {
    let id = route.params['id'];

    return this.pollService.getById(id)
      .map(poll => {
        if (poll) {
          return poll;
        } else {
          console.log("NOT FOUND!"); // TODO reroute
        }
      },
      err => {
        console.error(err);
      });
  }
}

My poll-detail ngOnInit() function:

  ngOnInit(): void {
    this.route.data.forEach((data: { poll: Poll }) => {
      this.poll = data.poll;
      this.sharePoll(this.poll);
    });
  }
AryanJ-NYC
  • 2,529
  • 2
  • 20
  • 27

2 Answers2

2

You can easily resolve multiple observables using withLatestFrom.

You can see how that works in this answer: https://stackoverflow.com/a/53704018/6572208

Gorgant
  • 381
  • 5
  • 7
0

If they are observables, you can join them by using the combineLatest operator and then return the stream. Here is an example: https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/combinelatest.md

Dragos Ionescu
  • 1,163
  • 1
  • 9
  • 12