在路由中传递对象 [英] Passing objects in routing
问题描述
我在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屋!