Currently im migrating my ionic 3 project to ionic 5, in ionic 3 we use this.navCtrl.getPrevious().name; to get the previous page name by getting the previous page name i will navigate to different pages based on the previous page name can you please help me how can i get previous page name in ionic 4
Asked
Active
Viewed 3,177 times
1
-
Does this answer your question? [Ionic 4 Angular Back Button to previous page instead of root?](https://stackoverflow.com/questions/54892266/ionic-4-angular-back-button-to-previous-page-instead-of-root) – Hamid Ali Jul 11 '20 at 20:58
-
No @HamidAli im faced an other issue but now solved as per below comments now it is working fine – Ajith Kumar Jul 13 '20 at 05:10
1 Answers
2
Ionic 4+ moved away from navCtrl and leverages Angular Router.
To read previous URL (route) you may do it via PreviousRouteService:
import { Injectable } from '@angular/core';
import { Router, RoutesRecognized } from '@angular/router';
import { filter, pairwise } from 'rxjs/operators';
@Injectable({
providedIn: "root"
})
export class PreviousRouteService {
private previousUrl: string;
private currentUrl: string;
constructor(private router: Router) {
this.currentUrl = this.router.url;
this.previousUrl = null;
this.router.events
.pipe(filter((event: any) => event instanceof RoutesRecognized), pairwise())
.subscribe((events: RoutesRecognized[]) => {
this.previousUrl = events[0].urlAfterRedirects;
this.currentUrl = events[1].urlAfterRedirects;
});
}
public getPreviousUrl() {
return this.previousUrl;
}
};
The service imports Router and tracks changes so that any component that needs previous URL information can import this service and access previous route:
constructor(
private previousRouteService: PreviousRouteService
) {}
const prevUrl = this.previousRouteService.getPreviousUrl();

Sergey Rudenko
- 8,809
- 2
- 24
- 51