ionic 4 get previous route
Getting the Previous Route in Ionic 4
To get the previous route in Ionic 4, you can use the Angular Router's router.events
to track the navigation history. Here's a code snippet that demonstrates how to achieve this:
import { Router, NavigationEnd } from '@angular/router';
export class YourComponent {
previousUrl: string;
constructor(private router: Router) {
this.router.events.subscribe(event => {
if (event instanceof NavigationEnd) {
this.previousUrl = this.router.url;
}
});
}
}
In this code, we subscribe to the router.events
and listen for the NavigationEnd
event to capture the previous URL when the navigation changes.
This approach allows you to track the previous route in Ionic 4 using Angular Router.