Remove a view from the back history - Ionic2

2019-03-11 04:49发布

Anyone knows how to remove a view from the back history (or navigation stack) in ionic2?

In Ionic 1 I solved this with

this.$ionicHistory.nextViewOptions({
  disableAnimate: true, 
  disableBack: true
});

Would be really useful, for example, to fully remove the login page of my application from the history once a successfully login was performed.

Just not showing the back button isn't enough in such case, since Android terminals got their own physical back button on the devices.

I tried, after my login function returned a successful promise and before pushing the next page in the stack:

this.navController.pop();

or

this.navController.remove(this.viewCtrl.index);

but unfortunately both weren't successful :(

标签: ionic2
3条回答
闹够了就滚
2楼-- · 2019-03-11 05:04

I got the same issue with Ionic 3.
So, only two steps to reset history:

// ...
constructor(public navCtrl: NavController) { }
// ...
this.navCtrl.setRoot(NewPageWithoutPrev);
this.navCtrl.popToRoot();
// ...

Links:
https://ionicframework.com/docs/api/navigation/NavController/#setRoot
https://ionicframework.com/docs/api/navigation/NavController/#popToRoot

查看更多
来,给爷笑一个
3楼-- · 2019-03-11 05:09

To remove one backview you need to use startIndex and count of pages to remove from stack.

    this.navCtrl.push(NextPage)
    .then(() => {
      const startIndex = this.navCtrl.getActive().index - 1;
      this.navCtrl.remove(startIndex, 1);
    });

See this document for more options like removeView(viewController): https://ionicframework.com/docs/v2/api/navigation/NavController/#remove

查看更多
我只想做你的唯一
4楼-- · 2019-03-11 05:16

obrejacatalin on the https://forum.ionicframework.com/t/solved-disable-back-in-ionic2/57457 found the solution

this.nav.push(TabsPage).then(() => {
  const index = this.nav.getActive().index;
  this.nav.remove(0, index);
});

so I guess it's important to push the next page first, wait for the promise answer and then remove the current view

查看更多
登录 后发表回答