Chase Martin
Chase Martin

Reputation: 51

(Ionic4) How to dynamically go to previous page?

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?

Upvotes: 0

Views: 1794

Answers (2)

Hassan Ali
Hassan Ali

Reputation: 1029

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();

}

Upvotes: 4

sebaferreras
sebaferreras

Reputation: 44659

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.

Upvotes: 1

Related Questions