我希望处理硬件后退按钮,因为我需要获取当前 View 的名称。
console.log(this.nav.getActive());
console.log(this.navCtrl.getActive().component.name);
这只是在设备上返回字母 t 但在浏览器中确实有效。
请告诉我如何在设备上获取页面/ View 名称以处理后退按钮
最佳答案
问题是 webpack 在设备上运行应用程序时正在缩小代码(我猜是使用 --prod 标志),这就是为什么在浏览器上工作时名称是正确的,但显示 t
作为在设备上运行应用程序时的名称。 为了避免这种情况,您可以使用 instanceof
运算符来检查页面是否是您要执行某些操作的页面。
这是我在我开发过的一个应用程序中处理后退按钮的方式:
this.platform.registerBackButtonAction(() => {
this.handleBackButton();
});
然后
private handleBackButton(): void {
// Prevent to dismiss a modal when another modal is being dismissed
let ready = true;
let activePortal = this.ionicApp._loadingPortal.getActive() ||
this.ionicApp._modalPortal.getActive() ||
this.ionicApp._toastPortal.getActive() ||
this.ionicApp._overlayPortal.getActive();
if (activePortal) {
ready = false;
activePortal.dismiss();
activePortal.onDidDismiss(() => { ready = true; });
return;
}
if (this.menuCtrl.isOpen()) {
this.menuCtrl.close();
return;
}
let view = this.navCtrl.getActive();
let page = view ? this.navCtrl.getActive().instance : null;
if (page && (page instanceof HomePage || page instanceof SignInPage)) {
this.platform.exitApp();
}
else if (this.navCtrl.canGoBack() || view && view.isOverlay) {
this.navCtrl.pop();
} else {
this.accountService.isLoggedIn() ? this.navCtrl.setRoot(HomePage) : this.navCtrl.setRoot(SignInPage);
}
}
正如您在代码中看到的,我使用 instanceof
运算符检查 View 是否为特定 View
page instanceof HomePage
即使 webpacks 压缩了代码,它也应该可以正常工作。
关于angular - 获取 RC0 中的当前页面/ View 名称返回 “t”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44562225/