Angular2 http请求取决于从其他响应返回的数据

Angular2 http请求取决于从其他响应返回的数据,angular,typescript,angular-services,Angular,Typescript,Angular Services,我创建了一个服务,它使用diff参数进行不同的http调用。 quote.service.ts getQuotes(){ let params = { "Type": "BasicDetail", } return this.http.post(this.url,params) .map(res => res.json()) } getOptions(){ let params = { "Type": "Hote

我创建了一个服务,它使用diff参数进行不同的http调用。 quote.service.ts

getQuotes(){
    let params = {
        "Type": "BasicDetail",
    }
    return this.http.post(this.url,params)
    .map(res => res.json())
}  

getOptions(){
    let params = {
        "Type": "Hoteloption",
    }
    return this.http.post(this.url,params)
    .map(res=>res.json())
}

getServiceWiseOptions(){
    let params = {
        "Type": "ServiceWiseOption",
    }
    return this.http.post(this.url,params)
    .map(res=>res.json())
}
getOption() {
 this.quoteService.getQuotes().mergeMap( quotes => {
  if(this.quotes.BasicDetails[0].QuotType == 'QUOTTYP-3'){
    return this.quoteService.getServiceWiseOptions()
  }
  else{
    return this.quoteService.getOptions()
  }
})
 .subscribe(
  options => {
    this.optionsdata = options.resultData;             
    if(this.quotes.BasicDetails[0].QuotType == 'QUOTTYP-3'){
      this.servicewiseData = options.resultData.ServiceWiseOption;
    }else{                
      this.servicewiseData = options.resultData.Hoteloption; 
    }    
  },
  )
}
我在
构造函数中的组件代码中调用下面的
getOption()

组件。ts

getQuotes(){
    let params = {
        "Type": "BasicDetail",
    }
    return this.http.post(this.url,params)
    .map(res => res.json())
}  

getOptions(){
    let params = {
        "Type": "Hoteloption",
    }
    return this.http.post(this.url,params)
    .map(res=>res.json())
}

getServiceWiseOptions(){
    let params = {
        "Type": "ServiceWiseOption",
    }
    return this.http.post(this.url,params)
    .map(res=>res.json())
}
getOption() {
 this.quoteService.getQuotes().mergeMap( quotes => {
  if(this.quotes.BasicDetails[0].QuotType == 'QUOTTYP-3'){
    return this.quoteService.getServiceWiseOptions()
  }
  else{
    return this.quoteService.getOptions()
  }
})
 .subscribe(
  options => {
    this.optionsdata = options.resultData;             
    if(this.quotes.BasicDetails[0].QuotType == 'QUOTTYP-3'){
      this.servicewiseData = options.resultData.ServiceWiseOption;
    }else{                
      this.servicewiseData = options.resultData.Hoteloption; 
    }    
  },
  )
}
我需要的是根据
getOption()
的响应从服务调用
getServiceWiseOptions()
,如果我得到
quot类型:
getServiceWiseOptions()
否则
getOptions()

上面的函数
getOption()
有时工作,但有时不调用这两个函数中的任何一个

请给我一些建议我该怎么办? 我认为它的问题在于
mergeMap()

在您的
getOption()
函数中,您使用
this
引用了您的响应,这使得它成为一个控制器变量,而不是
响应
数据

因此,将
this.quotes
更改为
quotes

getOption() {
 this.quoteService.getQuotes().mergeMap( quotes => {
    this.quotes = quotes; // this line if you want it to use anywhere else
  if(quotes.BasicDetails[0].QuotType == 'QUOTTYP-3'){
    return this.quoteService.getServiceWiseOptions()
  }
  else{
    return this.quoteService.getOptions()
  }
})
 .subscribe(
  options => {
    this.optionsdata = options.resultData;             
    if(this.quotes.BasicDetails[0].QuotType == 'QUOTTYP-3'){
      this.servicewiseData = options.resultData.ServiceWiseOption;
    }else{                
      this.servicewiseData = options.resultData.Hoteloption; 
    }    
  },
  )
}

你能发送
getQuotes()
方法的响应吗?使用
mergeMap
安装
map
有什么大的原因吗?@Sravan没有使用
mergeMap()的具体原因。我不知道有人叫我这么做,但那是行不通的well@Sravan根据当前代码,如果我不使用
mergeMap
获取错误
类型“Observable”上不存在属性“resultData”
getOption()中获取的
引号的结果是什么
response@Sravan好的,让我向您展示来自
getOption