ionic-framework - 当用户单击手机后退按钮时,ionic 4 处理模态
问题描述
当用户点击手机的后退按钮时会发生什么?如果模态打开。
注册了一个后退按钮:
// To prevent interference with ionic's own backbutton handling
// you can subscribe with a low priority instead
this.platform.backButton.subscribe(() => {
// code that is executed when the user pressed the back button
// and ionic doesn't already know what to do (close modals etc...)
self.modalController.dismiss();
});
代码的问题:
它关闭/关闭模式很好!
但它也将打开模式的页面推回。意味着它弹出模式后面的页面。
这不应该发生页面不应该弹出 - 只有模式应该关闭。
检查添加的图像 gif -> 单击此处查看问题
解决方案
您可以考虑使用platform.backButton.subscribeWithPriority()
高优先级(例如:9999)。
然后检查是否有一个打开的模式modalController.getTop()
。
constructor(private modalCtrl: ModalController, private nav: NavController) {
}
ngOnInit() {
this.platform.backButton.subscribeWithPriority(9999, () => {
this.closeModalOrPage();
});
}
async closeModalOrPage(){
let modal = await this.modalCtrl.getTop();
if (modal){
modal.dismiss();
} else {
this.nav.pop();
}
}
推荐阅读
- amazon-web-services - 速率限制 AWS CloudWatch 日志摄取
- css - 如何在内嵌圆圈上添加文本?
- pandas - 根据熊猫中2个不同列中的日期运行计数
- wordpress - 如果已发布的 WP 插件更改了名称和文件结构,将更新上传到 WP 存储库的最佳方式是什么?
- javascript - How to get interval or timeout by ID
- c# - 为什么这个 .net 核心 webapi 端点有 2 个类?
- javascript - React hooks - 在一个钩子中调用两个函数
- python - 如何使用 Google Calendar APIs 资源 __init__ 构造函数?
- python - 在 3D 数组上映射 scipy percentileofscore
- python - 使用海龟进行布冯针模拟。我不能让我的 if else 语句来改变针的颜色?