0

I am currently updating my company's app from Ionic v3 to Ionic v4. I am stuck on trying to figure out how to dynamically go back to the previous page the user was on. I keep finding answers of this.router.navigate(['pagename']) or this.navCtrl.navigateBack('pagename'), however, there are multiple entry points to a map in our app which is where the problem lies. We used to use this.navCtrl.pop which would bring the user back to whatever page they came from... is there an equivalent to this since I don't know which page the user will be coming from?

  • https://stackoverflow.com/questions/62847593/how-to-dynamically-go-to-previous-page-ionic-4/62851249#62851249 solution for to get previous page name – Ajith Kumar Jul 11 '20 at 17:09

2 Answers2

4

You must use these solutions. These will work on your ionic 4 project.

First import { Location } from '@angular/common';

private location: Location,

gotoBack() {
this.location.back();
}

Second is

import { NavController } from '@ionic/angular';

constructor(
private navCtrl: NavController
) { }

gotoBack() {
this.navCtrl.back();

}

Hassan Ali
  • 994
  • 6
  • 22
1

From the docs, seems like you could still use the pop() method in Ionic 4:

pop

Description: Pop a component off of the navigation stack. Navigates back from the current component.

Signature: pop(opts?: NavOptions | null | undefined, done?: TransitionDoneFn | undefined) => Promise<boolean>

So it'd be something like this.navCtrl.pop();

Note: Please notice that this may or may not work based on how you're handling the navigation (from the outlet, from a modal component, ...) so if this doesn't work please add some more information about how you're navigating to that map page in your app so that we can see what other options are available based on that.

sebaferreras
  • 44,206
  • 11
  • 116
  • 134