Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 291
Valeri Avatar asked Apr 10 '19 14:04

Valeri


2 Answers

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

constructor(private navCtrl: NavController) {}

btnClick(){
this.navCtrl.navigateBack('/home'); 
} 
like image 175
Peter Haddad Avatar answered Oct 13 '22 01:10

Peter Haddad


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

like image 30
Andrew Pomorski Avatar answered Oct 12 '22 23:10

Andrew Pomorski