2017-05-04 63 views
5

我有兩個組件。客戶和貨運組件。當用戶從Shipment組件到Customer組件時。我想將客戶組件直接發送到發貨組件。Angular 2得到前一頁的網址

我使用這種方法。 this._location.back(); from Location angular/common。

這種方法每個總是直接指向背頁。但是我希望直接來自發貨組件。

+1

您可以使用'pairwise',如http://stackoverflow.com/questions/33520043/how-to-detect-a-route-change-in-angular-2/38080657#38080657中所示,並存儲當前和以前的服務價值 –

回答

9

插入下面的代碼到你的父組件,其中包含你的兩個部分組成: -

import { Router, NavigationEnd } from "@angular/router"; 

previousUrl:string; 
constructor(private router: Router) { 

    router.events 
     .filter(event => event instanceof NavigationEnd) 
     .subscribe(e => { 
      this.previousUrl = e.url; 
     }); 
} 

它的工作對我來說,我希望它會爲你工作爲好。

+6

我試過這段代碼。但它返回當前的網址。 –

+0

將此代碼添加到您的父組件,其中包含您的2個組件 –

+0

我將代碼中的pairwise方法添加進去。它返回兩個當前和前一個url。但我沒有在任何地方使用這些網址。 –