Angular2 在另一个具有相同类的函数中调用函数,并在第二个函数中使用第一个函数属性



我在一个类中有 2 个函数,两个类都从 API 调用中获取数据。

这是第一个函数

getQuotes(){
this.quoteService.getQuotes()
.subscribe(
quotes => {
this.quotCode = this.quotes.BasicDetails[0].QuotCode;
}
});
}

第二个功能

getOption(){
this.quoteService.getOptions()
.subscribe(
options => {
})
}

我想从第一个函数的这一行获取数据

this.quotCode = this.quotes.BasicDetails[0].QuotCode;

并且基于this.quotCodegetOption()将有不同的参数要传递,并且也会得到不同的响应。 所以有必要在第二个函数中使用this.quotCode

我在constuctor()打电话给getQuotes()

谁能帮帮我? 谢谢

RXJSmergeMap是您需要的运算符:这样,一旦第一个请求成功,您将执行第二个请求。

this.quoteService.getQuotes().mergeMap( quotes => {
// Do whatever you need to do with quotes
return this.quoteService.getOptions()
}).subscribe( options => {
// Do whatever you need with options
});

最新更新