在路由中传递对象 [英] Passing objects in routing

查看:33
本文介绍了在路由中传递对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Angular2 中有一个组件托管用户表 (userTableComponnent) 和另一个 userDetails 组件.单击用户表中的一行后,我想路由到 userDetails.一种实现是仅传递 userId,因此在 userDetails 中,我使用另一个 http get 获取用户的详细信息.但是,这是多余的,因为我在 userTableComponent 中获取了所有用户信息.所以我真正需要的是将 User 对象从 userTableComponent 传递给 userDetails.知道如何通过路由实现它吗?

I have a component in Angular2 hosting the table of users (userTableComponnent) and another component of userDetails. Upon clicking on a row in users table, I want to route to the userDetails. One implementation is to pass the userId only, so in userDetails, I fetch the details of the user with another http get. However, this is redundant, as I grab all the user info in userTableComponent. So what I really need is to pass the User object from userTableComponent to userDetails. Any idea how to achieve it through routing?

推荐答案

您可以创建一个 SessionService 类并通过依赖注入将其传递给您的应用程序.

You can create a SessionService class and pass it around your App via Dependency Injection.

选择用户后,您可以将相关数据附加到通过 DI 注入的 SessionService 实例,并在 userDetails 组件中检索它.

Once the user is selected you can attach the relative data to the SessionService instance injected via DI and retrieve it in the userDetails Component.

希望对你有帮助

这篇关于在路由中传递对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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