通过 Ionic 4 上的硬件后退按钮关闭应用程序
Close the app by hardware back button on Ionic 4
我正在尝试让我的应用程序在后退按钮上关闭
我在 github
中找到了这段代码
//Called when view is loaded as ionViewDidLoad() removed from Ionic v4
ngOnInit(){
this.initializeBackButtonCustomHandler();
}
//Called when view is left
ionViewWillLeave() {
// Unregister the custom back button action for this page
this.unsubscribeBackEvent && this.unsubscribeBackEvent();
}
initializeBackButtonCustomHandler(): void {
this.unsubscribeBackEvent = this.platform.backButton.subscribeWithPriority(999999, () => {
alert("back pressed home" + this.constructor.name);
});
/* here priority 101 will be greater then 100
if we have registerBackButtonAction in app.component.ts */
}
有效,但适用于所有页面
我只想在主页上限制它
我用了 this.constructor.name == HomePage
但没用
上面的代码有效,但我认为你写错了地方。在主页的 OnInit 或 ionViewDidEnter 方法上订阅 BackButton,在 OnDestroy 或 ionPageWillLeave 上取消订阅。
否则:您不必需要这么高的优先级。默认值为零。每个更高的都会工作。如果您在多个页面和组件上使用此方法,您应该考虑优先级逻辑。
我正在尝试让我的应用程序在后退按钮上关闭 我在 github
中找到了这段代码 //Called when view is loaded as ionViewDidLoad() removed from Ionic v4
ngOnInit(){
this.initializeBackButtonCustomHandler();
}
//Called when view is left
ionViewWillLeave() {
// Unregister the custom back button action for this page
this.unsubscribeBackEvent && this.unsubscribeBackEvent();
}
initializeBackButtonCustomHandler(): void {
this.unsubscribeBackEvent = this.platform.backButton.subscribeWithPriority(999999, () => {
alert("back pressed home" + this.constructor.name);
});
/* here priority 101 will be greater then 100
if we have registerBackButtonAction in app.component.ts */
}
有效,但适用于所有页面
我只想在主页上限制它
我用了 this.constructor.name == HomePage
但没用
上面的代码有效,但我认为你写错了地方。在主页的 OnInit 或 ionViewDidEnter 方法上订阅 BackButton,在 OnDestroy 或 ionPageWillLeave 上取消订阅。
否则:您不必需要这么高的优先级。默认值为零。每个更高的都会工作。如果您在多个页面和组件上使用此方法,您应该考虑优先级逻辑。