【发布时间】:2019-11-07 20:01:18
【问题描述】:
我有这样的功能:
getNumber(value) {
this.myService.getApi(value).subscribe(data => {
this.myVariable = data;
});
}
我在其他函数中调用该函数如下:
set() {
if (this.value == 1) {
this.getNumber(firstNumber)
this.newVariable = this.myVariable;
} else {
this.getNumber(secondNumber)
this.newVariabe = this.myVariable + 1;
}
};
this.value 正在从其他函数获取。问题是this.newVariable 是空的。我确定这是因为在我尝试访问this.Variable之前订阅尚未完成
在我做任何其他事情之前有什么方法可以等待订阅完成?
【问题讨论】:
-
getNumber应该返回 Observable,并且您应该在需要该值时在“其他”函数中订阅它。