简体   繁体   English

Ionic 2自定义后退按钮动作

[英]Ionic 2 customize back button action

I want to customize the click-action of the back button mentioned in this screen capture. 我想自定义此屏幕截图中提到的后退按钮的单击操作。 I want that by clicking I do not return to the previous page but to a page that I specify myself, or do a treatment before going back. 我希望通过点击我不会返回到上一页,而是返回到我自己指定的页面,或者在返回之前进行处理。

截图

For customize default back button action you need override the backButtonClick() method of the NavBar component. 对于自定义默认后退按钮操作,您需要覆盖NavBar组件的backButtonClick()方法。

Step 1: In your "custom-class.ts" import Navbar component. 第1步:“custom-class.ts”中导入Navbar组件。 Create auxMethod for override the default behavior and called in your ionViewDidLoad method. 创建auxMethod以覆盖默认行为并在ionViewDidLoad方法中调用。

import { Navbar } from 'ionic-angular';
import { ViewChild } from '@angular/core';

export class myCustomClass {
    @ViewChild(Navbar) navBar: Navbar;

    ionViewDidLoad() {
        this.setBackButtonAction()
    }

    //Method to override the default back button action
    setBackButtonAction(){
       this.navBar.backButtonClick = () => {
       //Write here wherever you wanna do
          this.navCtrl.pop()
       }
    }
}

This code has been tested in ionic 3. 此代码已在离子3中进行了测试。

You can try to use ionViewCanLeave or ionViewWillLeave event. 您可以尝试使用ionViewCanLeaveionViewWillLeave事件。

See this issue #9533 with proposal to distinguish leave events for "back" navigation. 请参阅此问题#9533,并建议区分“返回”导航的离开事件。 This can be handy for your use case once implemented. 一旦实现,这对于您的用例非常方便。

You need to just remove the current index from stack of ViewController 您需要从ViewController堆栈中删除当前索引

import { ViewController} from 'ionic-angular'; 从'ionic-angular'导入{ViewController};

      constructor(public navCtrl: NavController, public viewCtrl: ViewController) {
      }


this.navCtrl.push("APage").then(() => {
      const index = this.viewCtrl.index;
      this.navCtrl.remove(index,1);
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM