Angular 2-通过路由器链接将对象从一个组件发送到另一个组件 [英] Angular 2 - send object from one component to another over router-link

查看:58
本文介绍了Angular 2-通过路由器链接将对象从一个组件发送到另一个组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过角度为2的路由器链接发送对象. 我为人员阵列人员中的每个人员对象创建人员配置文件组件,并将其显示在屏幕上.

Im trying to send an object over router-link in angular 2. I create a person-profile component for every person object in my person array people and display them on my screen.

<div *ngFor="#person of people; #i = index">
    <person-profile  [person]="person"></person-profile>
</div>

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

The person-profile component displays several pieces of information contained in the person object. Im trying to send the entire person object from the person-profile component to a component named "map" using a router-link and passing the person object as a parameter.

<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>

在我的地图组件中,我使用以下方法检索对象:

In my map component i retrieve the object with:

var person = <any> routeParams.get('person');

问题在于,无论我从哪个<person-profile>执行路由器链接,每次在地图组件中都得到同一个人.它始终是人员列表中的第一人.奇怪的是,如果我从person对象传递特定的参数,它将起作用.例如,如果我通过person.family_name而不是整个person对象,则一切正常,但是我想发送整个对象.有什么建议吗?

The problem is that i get the same person every time in the map component regardless of which <person-profile> i execute the router-link from. Its always the first person in the people list. The weird thing is that if i pass specific parameters from the person object it works. For instance, if i pass person.family_name instead of the the entire person object everything works but i want to send the entire object. Any suggestions?

谢谢!

推荐答案

AFAIK无法传递对象,因为RouterLink类的源不表示对此提供任何支持,并且因为需要显示数据在URL字符串中.

AFAIK there is no way to pass objects because the source of the RouterLink class doesn't indicate any support for that and because the data needs to be presented in the URL string.

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

My suggestion is to pass an ID and share the users in a service that allows to fetch the concrete instance by ID.

这篇关于Angular 2-通过路由器链接将对象从一个组件发送到另一个组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆