Angular Ionic 2防止硬件后退按钮默认值
按下“硬件后退”按钮时,如何防止默认导航?我尝试了Angular Ionic 2防止硬件后退按钮默认值,angular,typescript,ionic-framework,ionic2,ionic3,Angular,Typescript,Ionic Framework,Ionic2,Ionic3,按下“硬件后退”按钮时,如何防止默认导航?我尝试了注册BackButtonAction,但它会覆盖我不想要的每个页面中的后退按钮行为 这也没用 document.addEventListener("backbutton", (event) => { event.preventDefault(); }, false); 正如您在注册表BackButtonAction中看到的,返回一个函数: 一个函数,当被调用时,它将注销其后退按钮 行动 因此,您可以使用该函数在离开页面时恢
注册BackButtonAction
,但它会覆盖我不想要的每个页面中的后退按钮行为
这也没用
document.addEventListener("backbutton", (event) => {
event.preventDefault();
}, false);
正如您在注册表BackButtonAction
中看到的,返回一个函数:
一个函数,当被调用时,它将注销其后退按钮
行动
因此,您可以使用该函数在离开页面时恢复默认行为,如下所示:
import { Component} from '@angular/core';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
// Property used to store the callback of the event handler to unsubscribe to it when leaving this page
public unregisterBackButtonAction: any;
constructor(...) { ... }
ionViewDidEnter() {
this.initializeBackButtonCustomHandler();
}
ionViewWillLeave() {
// Unregister the custom back button action for this page
this.unregisterBackButtonAction && this.unregisterBackButtonAction();
}
public initializeBackButtonCustomHandler(): void {
this.unregisterBackButtonAction = this.platform.registerBackButtonAction(() => {
this.customHandleBackButton();
}, 10);
}
private customHandleBackButton(): void {
// do what you need to do here ...
}
}
如您所见,关键是存储registerBackButtonAction
方法的回调,并在以后离开页面时使用它(或当您希望恢复默认行为时):
非常感谢!我曾想过以某种方式将其存储为事件侦听器的顺序处理程序,但我不知道如何在离开时注销它们。很高兴听到这有帮助:)希望看到samethanks的TypeScript版本用于您的解决方案,但我需要双击硬件后退按钮还原后退按钮默认行为。拜托,我该怎么办?
this.unregisterBackButtonAction = this.platform.registerBackButtonAction(() => {
this.customHandleBackButton();
}, 10);