如何在角度2中获取其他组件中的多个参数

时间:2017-09-06 05:24:50

标签: angular2-routing

这是一个ts文件中的函数,我传递纬度和经度。现在我必须在其他组件中获取这些lat long,以便如何在其他组件中获取这些参数。

routeWithData(id, latitude, longitude)
   {   
      //console.log(latitude, longitude);
      this.router.navigate(['/provider/bookings/'+id+'/details/map-location/'+latitude+'/'+longitude]);     
   }

2 个答案:

答案 0 :(得分:0)

如果您使用router.navigate导航([' / someroute',{someProperty:" SomeValue"}]

按照:

   constructor(routeParams: RouteParams){ let myPassedData: any=routeParams.params;console.log(myPassedData.someProperty); #Prints"SomeValue"}

您可以从这里了解如何使用它: how to pass RouteData via Router.navigate in angular2

答案 1 :(得分:0)

您可以通过传递URL中的所有参数来导航

routeWithData(id, latitude, longitude) {   
      //console.log(latitude, longitude);
      this.router.navigate(['/provider/bookings/'+id+'/details/map-location/'+latitude+','+longitude]);     
   }

在这种情况下,您必须解码URL以获取纬度和经度。

或者您可以在查询参数中传递参数:

routeWithData(id, latitude, longitude){   
      //console.log(latitude, longitude);
      lat latlon = latitude + ', ' + longitude;
      this.router.navigate(['/provider/bookings/'+id+'/details/map-location/', { queryParams: { latlon: latlon } }]);     
   }

有关详细信息,请参阅https://angular.io/guide/router#query-parameters-and-fragments