Skip to content

Instantly share code, notes, and snippets.

@lexeek
Created October 18, 2018 02:48
Show Gist options
  • Save lexeek/e31d1806d3d7fdd76afb241b0697b742 to your computer and use it in GitHub Desktop.
Save lexeek/e31d1806d3d7fdd76afb241b0697b742 to your computer and use it in GitHub Desktop.
Angular 6+. Access previous route
import { Injectable } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';
@Injectable()
export class PreviousRouteService {
private previousUrl: string;
private currentUrl: string;
constructor(private router: Router) {
this.currentUrl = this.router.url;
router.events.subscribe(event => {
if (event instanceof NavigationEnd) {
this.previousUrl = this.currentUrl;
this.currentUrl = event.url;
};
});
}
public getPreviousUrl() {
return this.previousUrl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment