首页 > 解决方案 > 避免或改进倾向于依赖于先前调用结果的角度服务调用

问题描述

我有一个简单的表单,用户输入代码并单击搜索按钮:

  1. 搜索逻辑

    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;
        });
}

对此类服务的调用就像瀑布一样,只有在以下情况下才能获取下一步的信息:

  1. 数据可从第一次服务调用中获得
  2. 如果第一次调用的响应中有特定的键

在 Angular 中这种情况有更好的做法吗?可能存在这样一种情况,如果第一个端点发生故障,整个组件将停止运行。

标签: angular

解决方案


推荐阅读