6

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

3

3 Answers 3

7

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

constructor(private navCtrl: NavController) {} btnClick(){ this.navCtrl.navigateBack('/home'); } 
Sign up to request clarification or add additional context in comments.

3 Comments

Wouldn't it be more appropriate to use location.back() from @angular/common when OP is using angular routing? (see angular.io/api/common/Location)
It is being called when u usenavigateBack(), ionic uses it in there code
I just realized that when you use location.back() directly you don't get a proper back animation.
5

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

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.