Ionic 2 customize back button action

雨燕双飞 提交于 2019-11-30 09:50:15
Ariel Antonio Fundora

For customize default back button action you need override the backButtonClick() method of the NavBar component.

Step 1: In your "custom-class.ts" import Navbar component. Create auxMethod for override the default behavior and called in your ionViewDidLoad method.

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.

You can try to use ionViewCanLeave or ionViewWillLeave event.

See this issue #9533 with proposal to distinguish leave events for "back" navigation. This can be handy for your use case once implemented.

You need to just remove the current index from stack of ViewController

import { ViewController} from 'ionic-angular';

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


this.navCtrl.push("APage").then(() => {
      const index = this.viewCtrl.index;
      this.navCtrl.remove(index,1);
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!