Created
October 18, 2018 02:48
-
-
Save lexeek/e31d1806d3d7fdd76afb241b0697b742 to your computer and use it in GitHub Desktop.
Angular 6+. Access previous route
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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