簡體   English   中英

registerBackButtonAction,Ionic2,用於不同的頁面

[英]registerBackButtonAction , Ionic2 , for different pages

我正在開發一個Ionic2應用程序。 我對registerBackButtonAction功能感到困惑。

我在一個頁面上做了這個(比如pageA)。 它按預期工作。

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

現在我想在其他頁面(例如PageB)上對registerBackButtonAction做一些其他操作。 但是Ionic正在從pageA采取行動

如何在不同頁面上注冊不同的作品。

正如你在Ionic docs中看到的那樣, registerBackButtonAction返回一個函數:

一個函數,當被調用時,將取消注冊其后退按鈕操作。 因此,您可以使用該功能在離開頁面時恢復默認行為,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM