Angular2 http 请求依赖于从其他响应返回的数据



我创建了一个服务,它与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()constructor

组件.ts

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

我需要的是getServiceWiseOptions()调用并根据getOption()的响应从服务getOptions(),如果我得到QuotType:getServiceWiseOptions()否则getOptions().

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

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

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

所以把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; 
}    
},
)
}

最新更新