【发布时间】:2023-04-10 00:21:01
【问题描述】:
我正在开发一个组件,我需要在其中并行调用两个 API。而且我只想在两个 API 调用都解决后才调用一个方法。
ngOnInit() {
this.serviceOne.someApi().subscribe((res) => {
this.response1 = res;
});
this.serviceTwo.someApi().subscribe((res) => {
this.response2 = res;
});
}
我只想在 this.response1 和 this.response2 被填充时调用方法。
目前,我使用if statement 将逻辑封装在方法中。
someMethod() {
if(this.response1 && this.response2) {
//logic
}
}
并将someMethod() 放入两个 api 调用的 subscribe() 中。
实现这一目标的最佳方法是什么?
【问题讨论】:
标签: angular typescript