Imad El Hitti
Imad El Hitti

Reputation: 937

Return multiple information from angular route resolver

I'm facing an issue concerning the return of the resolver in my application.

Basically that's my resolver :

constructor(private authService: AuthService) { }
  resolve() {
    /*
    this.authService.token; // (property) PeAuthService.authorizationHeader: string
    this.authService.getUserInfo(); // (method) PeAuthService.getUserInfos(): Observable<any>
    */
    return {
      token: this.authService.token,
      userInfo: this.authService.getUserInfo()
    };
  }

I dont't feel like I'm doing the correct way, because I can access the token value but not the userInfo.

Is there any way to return one observable containing userinfo data and the token ? So maybe combining an observable and a string in one observable ?

Upvotes: 1

Views: 521

Answers (2)

martin
martin

Reputation: 96891

More "Rx" way would be getting getUserInfo() and then combining it with this.authService.token using the map() operator:

this.authService.getUserInfo()
  .pipe(
    map(userInfo => ({
      userInfo,
      token: this.authService.token,
    }))
  );

Upvotes: 1

David Walschots
David Walschots

Reputation: 12640

Without awaiting the value, the userInfo property of the returned object contains the Observable. Therefore, simply use async and await:

async resolve() {
  return {
    token: this.authService.token,
    userInfo: await this.authService.getUserInfo()
  };
}

Upvotes: 0

Related Questions