Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 get previous page url

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.

like image 936
NadirCan KAVKAS Avatar asked May 04 '17 08:05

NadirCan KAVKAS


1 Answers

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.

like image 116
Yashwardhan Pauranik Avatar answered Oct 04 '22 17:10

Yashwardhan Pauranik