当按下“硬件后退”按钮时,如何防止默认导航?我尝试过registerBackButtonAction,但是它覆盖了我不想要的每一页中back按钮的行为。
这也没什么用。
document.addEventListener("backbutton", (event) => {
event.preventDefault();
}, false);发布于 2017-02-27 15:18:57
正如您在离子文档中看到的,registerBackButtonAction返回一个函数:
一个函数,当被调用时,它将注销它的back按钮操作。
因此,您可以在离开页面时使用该函数恢复默认行为,如下所示:
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);https://stackoverflow.com/questions/42483627
复制相似问题