加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular – registerBackButtonAction,Ionic2,用于不同的页面

发布时间:2020-12-17 09:03:59 所属栏目:安全 来源:网络整理
导读:我正在开发一个Ionic2应用程序.我对registerBackButtonAction功能感到困惑. 我在一个页面上做了这个(比如pageA).它按预期工作. this.platform.registerBackButtonAction(() = { console.log("back presed"); this.abortDownloadAndExit(); }); 现在我想在其
我正在开发一个Ionic2应用程序.我对registerBackButtonAction功能感到困惑.

我在一个页面上做了这个(比如pageA).它按预期工作.

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

现在我想在其他页面(例如PageB)上对registerBackButtonAction做一些其他操作.但Ionic正在从pageA采取行动

如何在不同页面上注册不同的作品.

正如您在 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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读