2
如何在按下硬件后退按钮时防止默认导航?我试过registerBackButtonAction
,但是它覆盖了我不想要的每个页面中后退按钮的行为。Ionic 2防止硬件后退按钮默认
这也没有帮助。
document.addEventListener("backbutton", (event) => {
event.preventDefault();
}, false);
如何在按下硬件后退按钮时防止默认导航?我试过registerBackButtonAction
,但是它覆盖了我不想要的每个页面中后退按钮的行为。Ionic 2防止硬件后退按钮默认
这也没有帮助。
document.addEventListener("backbutton", (event) => {
event.preventDefault();
}, false);
正如你在Ionic docsregisterBackButtonAction
回报看功能:
,调用它时,将注销其后退按钮 动作的功能。
所以,你可以使用该功能来恢复默认行为,离开页面的时候,像这样:
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
方法的回调稍后在离开页面时使用它(或者当您想恢复默认行为时):
this.unregisterBackButtonAction = this.platform.registerBackButtonAction(() => {
this.customHandleBackButton();
}, 10);
非常感谢!我曾想过如何将它存储为事件监听器的序号处理程序,但我不知道如何在离开时取消注册。 –
很高兴听到它帮助:) – sebaferreras
很想看到TypeScript版本相同 –