【问题标题】:angular resolve with nested promise and observable具有嵌套承诺和可观察的角度解析
【发布时间】:2019-07-22 10:11:44
【问题描述】:

我在一个组件中使用带有路由的解析,但在解析中,一个 HTTP 调用依赖于另一个 Promise 调用。

resolve(){
  return this.storage.getUser().then( user => {
    this.getVendorDetails(user.login);
  });
}

getVendorDetails(loginId) {
  return this.http.get('http://localhost:8080/user/getVendor/' + loginId);
}

当我试图从ActivatedRoute获取数据时在组件中

ngOnInit() {
  this.activatedRoute.data.subscribe( data => {
    console.log(data);
  });
}

我收到undefined。也许我在解析功能中遗漏了一些东西。 如何使用ActivatedRoutegetVendorDetails() 回复到ngOnInit()

【问题讨论】:

    标签: angular


    【解决方案1】:

    要么将一个设为 observabe,要么将另一个设为 promise 并始终返回:

    resolve() {
      return this.storage.getUser().then(user => this.getVendorDetails(user.login).toPromise());
    }
    

    resolve() {
      return from(this.storage.getUser()).pipe(
        switchMap((user) => this.getVendorDetails(user.login))
      );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-02
      • 2020-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-17
      相关资源
      最近更新 更多