6

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.

NadirCan KAVKAS
  • 159
  • 1
  • 4
  • 15
  • 1
    You can use `pairwise` like shown in http://stackoverflow.com/questions/33520043/how-to-detect-a-route-change-in-angular-2/38080657#38080657 and store the current and previous value in a service – Günter Zöchbauer May 04 '17 at 09:01

1 Answers1

11

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.

Yashwardhan Pauranik
  • 5,370
  • 5
  • 42
  • 65