[英]Ionic 2 customize back button action
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. 您可以尝试使用ionViewCanLeave
或ionViewWillLeave
事件。
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.