Skip to content

Instantly share code, notes, and snippets.

@doblesesays
Created August 10, 2018 14:45
Show Gist options
  • Save doblesesays/b90ad84a43530510563dcaee59b4ac60 to your computer and use it in GitHub Desktop.
Save doblesesays/b90ad84a43530510563dcaee59b4ac60 to your computer and use it in GitHub Desktop.
Service for get the current and previous URL on Angular
import { UrlRouteService } from '...';
...
export class AppModule {
constructor(public urlRouteService: UrlRouteService){}
}
import { UrlRouteService } from '...';
...
constructor(
private urlRouteService: UrlRouteService
) {
this.urlRouteService.getPreviousUrl()
this.urlRouteService.getCurrentUrl()
}
...
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