Return 来自 angular 路由解析器的多个信息

Return multiple information from angular route resolver

我在我的应用程序中遇到了有关 return 解析器的问题。

基本上这就是我的解析器:

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()
    };
  }

我觉得我的做法不对,因为我可以访问令牌值但不能访问用户信息。

有什么方法可以 return 一个包含用户信息数据和令牌的可观察对象吗?那么也许可以将一个可观察对象和一个字符串组合成一个可观察对象?

无需等待值,返回对象的 userInfo 属性 包含 Observable。因此,只需使用 asyncawait:

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

更多 "Rx" 方法是获取 getUserInfo(),然后使用 map() 运算符将其与 this.authService.token 组合:

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