Angular2 http请求取决于从其他响应返回的数据
我创建了一个服务,它使用diff参数进行不同的http调用。 quote.service.tsAngular2 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
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