gpt4 book ai didi

angular - registerBackButtonAction , Ionic2 , 针对不同的页面

转载 作者:太空狗 更新时间:2023-10-29 17:22:39 27 4
gpt4 key购买 nike

我正在开发 Ionic2 应用程序。我对 registerBackButtonAction 功能感到困惑。

我已经在一个页面上完成了这个(比如 pageA)。并且它按预期工作。

this.platform.registerBackButtonAction(() => {
console.log("back presed");
this.abortDownloadAndExit();
});

现在我想对其他页面 (sayPageB) 上的 registerBackButtonAction 执行一些其他操作。但是 Ionic 正在从 pageA

采取行动

如何在不同的页面上注册不同的 Action 。

最佳答案

正如您在 Ionic docs 中看到的那样registerBackButtonAction 返回一个函数:

A function that, when called, will unregister its back button action. So you can use that function to restore the default behavior when leaving the page, like this:

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);

关于angular - registerBackButtonAction , Ionic2 , 针对不同的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43039523/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com