在subscribe()

时间:2019-04-05 14:31:42

标签: angular typescript

http get request得到响应后,我需要执行一些逻辑;理想情况下,请替换.then(),它不是一种可观察类型的方法,而是其后的所有方法。任何帮助表示赞赏。

// Getting app version
    this.http.get('assets/appversion.json').subscribe((version) => {
        runningVersion = version.data.version;

        const data = {
            mobileVersion: version.data.version,
            wsfunction: 'get_mobile_versioning',
            moodlewsrestformat : 'json'
        };

        const config = {
            params: data,
            headers : {'Accept' : 'application/json',
            }
        };

        return this.http.get
        (url, config);
    }).then((data) => { //RUN LOGIC AFTER RESPONSE FROM HTTP GET
        data.data.data.forEach((element) => {
            allowedVersionArray.push(element.allowed_versions);
        });

        allowedVersionArray.forEach((allowedVersion) => {
            if (allowedVersion === runningVersion) {
                isAllowedToRunApp = true;
            } else {
                // Redirect to allowed version app
            }
        });
    });

2 个答案:

答案 0 :(得分:2)

您应该像这样使用switchMap运算符:

this.http.get('assets/appversion.json').pipe(
  switchMap(version) => {
    runningVersion = version.data.version;

    const data = {
      mobileVersion: version.data.version,
      wsfunction: 'get_mobile_versioning',
      moodlewsrestformat : 'json'
    };

    const config = {
      params: data,
      headers : {'Accept' : 'application/json' }
    }

    return this.http.get(url, config);
  }).subscribe(
    (data) => { //RUN LOGIC AFTER RESPONSE FROM HTTP GET
      data.data.data.forEach((element) => {
        allowedVersionArray.push(element.allowed_versions);
      });

      allowedVersionArray.forEach((allowedVersion) => {
        if (allowedVersion === runningVersion) {
            isAllowedToRunApp = true;
        } else {
            // Redirect to allowed version app
        }
      });
  });

答案 1 :(得分:1)

您可以从第一个订阅中调用第二个订阅。理想情况下,您应该创建命名函数以使代码更清晰,更可维护,但这应该可行:

this.http.get('assets/appversion.json').subscribe(processVersion);

// Getting app version
function processVersion(version){
  runningVersion = version.data.version;

  const data = {
      mobileVersion: version.data.version,
      wsfunction: 'get_mobile_versioning',
      moodlewsrestformat : 'json'
  };

  const config = {
      params: data,
      headers : {'Accept' : 'application/json',
      }
  };

  this.http.get(url, config).subscribe(processData);
}

function processData(data){
  data.data.data.forEach((element) => {
    allowedVersionArray.push(element.allowed_versions);
  });

  allowedVersionArray.forEach((allowedVersion) => {
    if (allowedVersion === runningVersion) {
        isAllowedToRunApp = true;
    } else {
        // Redirect to allowed version app
    }
  });
}

您还应该记住要始终处理错误:

this.http.get('assets/appversion.json').subscribe(processVersion, errorOccurred);

// Getting app version
function processVersion(version){
  runningVersion = version.data.version;

  const data = {
      mobileVersion: version.data.version,
      wsfunction: 'get_mobile_versioning',
      moodlewsrestformat : 'json'
  };

  const config = {
      params: data,
      headers : {'Accept' : 'application/json',
      }
  };

  this.http.get(url, config).subscribe(processData, errorOccurred);
}

function processData(data){
  data.data.data.forEach((element) => {
    allowedVersionArray.push(element.allowed_versions);
  });

  allowedVersionArray.forEach((allowedVersion) => {
    if (allowedVersion === runningVersion) {
        isAllowedToRunApp = true;
    } else {
        // Redirect to allowed version app
    }
  });
}

function errorOccurred(error){
  throw `oh no! ${error} occurred!`;
}