Home Ask Login Register

Developers Planet

Your answer is one click away!

user3205675 February 2016

Angular2: How to find out what was previous page url when using angular2 routing

I am developing an angular2 app and I use router. I am in a page /myapp/signup. after signup I navigate the use to /myapp/login. /myapp/login can also be navigated from my home page which is /myapp. So now, when the user users /myapp/login I go back. This can be done by using location.back(). But I dont want to go back when the user is coming from /myapp/signup. So I should check if the user is coming from /myapp/signup and if so, I want to direct it to somewhere else. How can I know the previous url so that I direct the user to a specific state based on that ?


Bob van Luijt February 2016

You can do this with document.referrer

if(document.referrer !== '/myapp/signup'){

cexbrayat February 2016

You can maybe leverage the OnActivate phase with the routerOnActivate method that gives you access to the previous url and the current one.

routerOnActivate(next: ComponentInstruction, prev: ComponentInstruction) {
  console.log(`Finished navigating from "${prev ? prev.urlPath : 'null'}" to "${next.urlPath}"`);

Günter Zöchbauer February 2016

Inject Router and subscribe to events and store them for later reference

constructor(private _router: Router) {
  this._router.subscribe(route => {
    this.nextRoute ...
    this.prevRoute ...

Post Status

Asked in February 2016
Viewed 2,009 times
Voted 9
Answered 3 times


Leave an answer

Quote of the day: live life