Angular 2 - 通过路由器链接将对象从一个组件发送到另一个组件

Jak*_*son 5 angular2-routing angular

我试图通过角度为2的路由器链接发送一个对象.我为我的人阵列中的每个人对象创建一个人物 - 个人资料组件,并将其显示在我的屏幕上.

<div *ngFor="#person of people; #i = index">
    <person-profile  [person]="person"></person-profile>
</div>
Run Code Online (Sandbox Code Playgroud)

person-profile组件显示person对象中包含的几条信息.我试图使用路由器链接将人物对象组件中的整个人物对象发送到名为"map"的组件,并将人物对象作为参数传递.

<person-profile>
  ..code..
    <tr>
       <td class="category">Address</td>
       <td>{{ person.visiting_address }}</td>
       <td *ngIf="person.visiting_address"><a [routerLink]="['Map',{person:person}]">View on map</a></td>
    </tr>
..code..
<person-profile>
Run Code Online (Sandbox Code Playgroud)

在我的地图组件中,我检索对象:

var person = <any> routeParams.get('person');
Run Code Online (Sandbox Code Playgroud)

问题是我每次都在地图组件中得到同一个人,无论<person-profile>我从哪个执行路由器链接.它始终是人民名单中的第一人.奇怪的是,如果我传递来自person对象的特定参数,它就可以工作.例如,如果我传递person.family_name而不是整个person对象,一切正常,但我想发送整个对象.有什么建议?

谢谢!

Gün*_*uer 11

AFAIK没有办法传递对象,因为RouterLink类的源不表示对它的任何支持,因为数据需要在URL字符串中显示.

我的建议是传递一个ID并在允许按ID获取具体实例的服务中共享用户.