我不确定是否可以做到这一点,因此将尽力解释它。在只有Ionic 1的经验之后,我将跳到Ionic 4,并且涉及很多成长中的烦恼。在我的Ionic 1应用程序中,我有很多功能,其中包括离子弹出窗口,该弹出窗口对于功能的完成必不可少。
即,我正在使用类似于以下答案的代码:https://stackoverflow.com/a/55041136/5306408
我的总体代码的基本工作方式是:
基本上:
页面:
...
pageFunction() void {
this.someService.someFunction('someURL').then(dataFromTheURL) => {
//do something with dataFromTheURL
})
}
...
someService:
...
async openModal(params) {
const modal = await this.modalController.create({
component: someLoginModalPage,
componentProps: { ... }
});
modal.onDidDismiss().then((dataReturned) => {
//need a way to pass dataReturned back to pageFunction()
});
return await modal.present();
}
someFunction(url): Promise<any> {
return new Promise<any>((resolve) {
if (condition is fine and no modal is needed) {
this.http.get(url, {headers: someHeaderObject}).subscribe(result => {
resolve(result)
})
}
else {
//here is the crux of the problem - I need indicatorThatModalHasBeenClosed to come from the modal being closed, not opened
this.openModal(someParams).then((indicatorThatModalHasBeenClosed) => {
//do something with indicatorThatModalHasBeenClosed
this.http.get(url, {headers: someHeaderObject}).subscribe(result => {
resolve(result)
})
});
}
})
}
这可能与Angular模态有关吗?我很难找到它的任何示例,因为openModal
的代码返回的是modal.present()
的结果,而不是返回通过modal.onDidDismiss()
传递的数据。最终,我想要一个函数(返回Promise)来打开模式,以使用通过关闭相同模式提供的数据来解析。
答案 0 :(得分:0)
好吧,我想到了一种解决方法:如果模式打开,则拒绝someFunction
而不是解决它。
然后,将属性dataReturned
和loginModalActive
添加到someFunction
所属的服务中。
将this.loginModalActive = true;
添加到openModal
,并且
this.dataReturned = dataReturned;
this.loginModalActive = false;
转到随后包含的modal.onDidDismiss()
调用。
然后,将此功能添加到someFunction
所属的服务中:
waitForLoginModuleToClose() {
return new Promise(resolve => {
const delay = 100;
interval(delay).subscribe(() => {
if (!this.loginModalActive) {
resolve(this.dataReturned);
}
});
});
}
在页面中,如果pageFunction()
呼叫被拒绝,请someService
对其进行呼叫。
这并不是我想要的,但是它为服务创建了一种方式来跟踪是否应该打开模式,并每隔100毫秒运行一次间隔以检查其是否打开。一旦确定它没有打开,就可以再次尝试尝试的页面功能。