Angular 2 get previous page url

15,575

UPDATED for Angular 6

Insert the code below to your parent component, which contains your 2 components:-

import { Router, RoutesRecognized } from "@angular/router";
import { filter, pairwise } from 'rxjs/operators';

previousUrl:string;

constructor(private router: Router) {

    router.events
        .pipe(
          filter(event => event instanceof RoutesRecognized),
          pairwise()
        )            
        .subscribe((e: any) => {
            this.previousUrl = e[0].urlAfterRedirects;
        });
}

It worked for me, I hope it will work for you as well.

Share:
15,575
NadirCan KAVKAS
Author by

NadirCan KAVKAS

Updated on June 13, 2022

Comments

  • NadirCan KAVKAS
    NadirCan KAVKAS almost 2 years

    I have two components. Customer and Shipment components. When user come from Shipment component to Customer component. I want to Customer component direct to Shipment component.

    I use this method. this._location.back(); from Location angular/common.

    This method every always direct to back page. But I want direct to just when i came from shipment component.