angular - 避免或改进倾向于依赖于先前调用结果的角度服务调用
问题描述
我有一个简单的表单,用户输入代码并单击搜索按钮:
搜索逻辑
Search(code: string) { this.Backend.getMetaData(code) .then(resp => { // get information for the input code this.metaData = resp; // store response in the `metaData` var // if this particular key exists in the response from backend if ('productionProcessTemplate' in this.metaData) { // this.getBPInfo(resp['productionProcessTemplate']); this.getAnalysis(code); } // if this particular key exists in the response from backend if ('eventUrl' in resp) { this.getTableInfo(resp['eventUrl'], code); } }) .catch(error => { this.error_detc = true; }); }
因为对getAnalysis(code)
和的调用getTableInfo(..)
也是调用其他服务的函数,类似于Search(code)
getAnalysis(code)
getAnalysis(code) {
this.Backend.getAnalysisInfo(code)
.subscribe((res) => {
this.error_detc = false;
this.bpInfo = res;
}, (err) => {
this.error_detc = true;
});
}
getTableInfo(resp['eventUrl'], code)
getTableInfo(url, code) {
this.Backend.getTrackingInfo(url, code)
.subscribe((resp_track) => {
this.error_detc = false;
this.trackingInfo = resp_track.map(el => {
let _out = {
'eventTime': moment(el.eventTime.$date).utcOffset(el.eventTimeZoneOffset).toString(),
'bizStep': el.bizStep.split(':').pop(),
'action': el.action,
'readPoint': el.readPoint.id.split(':').pop(),
'bizLocation': ''
};
if ('bizLocation' in el) {
_out['bizLocation'] = el.bizLocation.id.split(':').pop();
return _out;
}
return _out;
});
}, (err) => {
this.error_detc = true;
});
}
对此类服务的调用就像瀑布一样,只有在以下情况下才能获取下一步的信息:
- 数据可从第一次服务调用中获得
- 如果第一次调用的响应中有特定的键
在 Angular 中这种情况有更好的做法吗?可能存在这样一种情况,如果第一个端点发生故障,整个组件将停止运行。
解决方案
推荐阅读
- typescript - 在 pm2 旁边编译一些脚本
- python - 我应该如何从这个网站上抓取 href 链接?
- amazon-web-services - AWS:不能在一次更新中执行多个 GSI 创建或删除
- java - 如果 Reactor Flux 未在设定的时间内完成,则抛出异常
- python - 在 Python 中使用入口点时区分导入的包和在 CLI 中运行的包
- python - DeblurGan 输出 - 奇怪的颜色
- node.js - 发生错误:'spawn ffmpeg ENOENT'、Node.js & FFmpeg
- ios - 在视图加载/刷新时调用 SwiftUI 视图扩展
- java - 在 Dagger 中创建对象
- vimeo - 一页上的多个 vimeo 视频的自定义控件