如何解决使用离子构建混合移动应用程序并面临硬件后退按钮问题
我正在使用 ionic 开发移动应用程序。我面临的问题是,当我单击硬件后退按钮时,它会将我带到菜单屏幕,我希望出现显示“您确定要退出吗?”的对话框,尽管我已经添加了代码仅当我触摸应用程序中的任何地方时才工作,但如果我没有触摸任何地方,它会将我带到我想避免的菜单屏幕,因为我想在单击后退按钮时显示对话框。我怎样才能做到这一点?我已在 Component.ts 文件中的登录、仪表板和欢迎页面中添加了此代码。代码如下:
ionViewDidEnter() {
this.platform.backButton.subscribeWithPriority(0,() => {
if (this.routerOutlet && this.routerOutlet.canGoBack()) {
this.routerOutlet.pop();
} else if (this.router.url === '/login' || this.router.url === '/dashboard'
|| this.router.url === '/welcome') {
var exitSure = confirm("Are you sure you want to exit?");
if (exitSure) {
navigator['app'].exitApp();
}
} else {
this.location.back();
}
});
}
解决方法
此代码可能对您有所帮助。
该对话框仅在登录页面、仪表板页面、欢迎页面显示。
app.component.ts
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();
this.platform.backButton.subscribe(async () => {
if (this.router.isActive('/login',true) && this.router.url === '/login') {
//Your dialog box Cod
navigator['app'].exitApp();
} else if (this.router.isActive('/dashboard',true) && this.router.url === '/dashboard') {
//Your dialog box Cod
navigator['app'].exitApp();
} else if (this.router.isActive('/welcome',true) && this.router.url === '/welcome') {
//Your dialog box Cod
navigator['app'].exitApp();
}
});
});
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。