我在一个组件中使用带有路由的解析,但在解析中,一个 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
。也许我在 resolve 函数中遗漏了一些东西。
如何使用 ActivatedRoute
将 getVendorDetails()
响应发送到 ngOnInit()
。
最佳答案
要么让一个成为 observbe,要么让另一个成为 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))
);
}
关于具有嵌套 promise 和可观察性的 Angular 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57143724/