Valeri
Valeri

Reputation: 1142

How to go back in Ionic 4

I want to add a back button in Ionic4(Angular 7). But I can't find the proper method in Angular Router.

import {Router} from '@angular/router';

How do we go back when clicking a button, in the component handler? I'd like to implement it using '@angular/router' not '@angular/common' => Location

Upvotes: 6

Views: 10869

Answers (3)

KevinTenyRoy
KevinTenyRoy

Reputation: 11

Just do the following if you wanna go back to your previous route.

constructor(private navCtrl: NavController) {}

goBack(){
 this.navCtrl.pop(); 
} 

This'll pop the current page from the navigation stack and return you to the previous page and the part of the page from where you had navigated forward.

Upvotes: 1

Andrew Pomorski
Andrew Pomorski

Reputation: 121

With Angular routing you could use the Location API:

constructor(private location: Location){}

and then when you need to navigate back call:

this.location.back();

Keep in mind that for Angular 7 you have to import Location from @angular/common

Upvotes: 5

Peter Haddad
Peter Haddad

Reputation: 80914

Since you are using ionic 4 then to go backward, you can do the following:

constructor(private navCtrl: NavController) {}

btnClick(){
this.navCtrl.navigateBack('/home'); 
} 

Upvotes: 7

Related Questions