【发布时间】:2019-07-17 16:27:57
【问题描述】:
我希望有人可以就如何执行以下操作提供一些建议:
我需要链接多个 http 请求,每个请求取决于前一个的结果。
即
第 1 步:登录用户
第 2 步:经过身份验证的 Http 请求
第 3 步:经过身份验证的 Http 请求以及来自第 2 步的响应值
第 4 步:经过身份验证的 Http 请求以及来自第 3 步的响应值
到目前为止,我有以下代码(有效):
this.authSandbox.login(this.loginForm).subscribe(() => {
this.customerEnquirySandbox.createCustomer(newCustomer)
.filter(response => !!response) // continue when response not null
.subscribe((response) => {
let response1Details = {
"id": response.data.id,
};
this.customerEnquirySandbox.createMeasure(response1Details )
.filter(response => !!response)
.subscribe((response) => {
let response2Details = {
"id": response.data.id,
};
this.customerEnquirySandbox.saveAnswers(response2Details )
.filter(response => !!response)
.subscribe((response) => {
if (response.data.success) {
alert('Form completed and answers saved successfully');
} else {
alert('Error submitting answers');
}
this.authSandbox.customerEnquirylogout();
});
});
});
});
上述方法可行,但似乎不是最合适的方法。我似乎建议使用 switchMap,但不确定这是否是正确的方法。如果有人可以就如何更正确地执行上述操作提供任何指导,我们将不胜感激。
提前致谢
【问题讨论】:
-
使用 switchMap 操作符
-
@JBNizet 感谢您的回复...是的,我知道这是正确的方法,但我无法重构上面的代码以使其工作
标签: angular rxjs httprequest subscribe switchmap