new Promiseresolve = zip thishttpClientgetassetstmpi18n$thisi18ndefaultLangjson thishttpClientgetassetstmpapp-datajson thishttpClientpost https101721161441450menusg
上面的代码使用了Promise和HttpClient来发送三个请求,并使用zip操作符将三个请求合并成一个Observable。标准的写法如下:
new Promise((resolve, reject) => {
zip(
this.httpClient.get(`assets/tmp/i18n/${this.i18n.defaultLang}.json`),
this.httpClient.get("assets/tmp/app-data.json"),
this.httpClient.post(
"https://10.172.116.144:1450/menus/getMenusByEmpNO",
{ EmpNo: user.account, SysId: "30326" },
{ headers: { "Content-Type": "application/json;charset=UTF-8" } }
)
).subscribe(
([i18nResponse, appDataResponse, menusResponse]) => {
// 处理响应数据
resolve([i18nResponse, appDataResponse, menusResponse]);
},
error => {
// 处理错误
reject(error);
}
);
});
这里使用了Promise来包裹Observable,并在subscribe方法中将响应数据传递给resolve函数。如果有错误发生,则使用reject函数传递错误信息
原文地址: https://www.cveoy.top/t/topic/iyD4 著作权归作者所有。请勿转载和采集!