Created
August 10, 2018 14:45
-
-
Save doblesesays/b90ad84a43530510563dcaee59b4ac60 to your computer and use it in GitHub Desktop.
Service for get the current and previous URL on Angular
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 { UrlRouteService } from '...'; | |
... | |
export class AppModule { | |
constructor(public urlRouteService: UrlRouteService){} | |
} |
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 { UrlRouteService } from '...'; | |
... | |
constructor( | |
private urlRouteService: UrlRouteService | |
) { | |
this.urlRouteService.getPreviousUrl() | |
this.urlRouteService.getCurrentUrl() | |
} | |
... |
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 UrlRouteService { | |
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; | |
} | |
public getCurrentUrl() { | |
return this.currentUrl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment