要防止在Angular 2中进行布线时重新创建组件
假设我们有两条路线Dashboard
和Profile
. Dashboard
具有动态标签功能,例如Google spreadsheet
.我想进行一些交互(构建图表,可视化一些数据),以在Dashboard
中创建选项卡.现在,如果我路由到Profile
然后又路由回Dashboard
,我想查看Dashboard
中这些选项卡中的内容.这意味着,我想在客户端维护状态. AFAIK在组件之间进行路由时会重新创建组件.使用angular 2路由时是否可以使电子表格类似应用程序?我需要使用路由,因为在我的应用程序中必须使用LazyLoading功能.
Let's say we have two routes Dashboard
and Profile
. Dashboard
has dynamic tabs feature like Google spreadsheet
. I want to do some interactions(build charts, visualizing some data) creating tabs in the Dashboard
. Now, if I route to Profile
and then route back to Dashboard
, I want to see what was before in those tabs in Dashboard
. It means, I want to maintain the state in the client side. AFAIK while routing between components, it recreates components. Is it possible to make spreadsheet like application while using angular 2 routing? I need to use routing because in my application I have to use LazyLoading feature.
那应该是什么主意?我刚接触Angular 2.
So what should be the idea? I am new to angular 2.
当前,当仅在保持相同路径的同时更改了路径参数时,仅仅重用了组件.
Currently components are reused only when only route parameters change while staying on the same route.
如果更改了路由,则在新路由添加相同组件时发生事件,该组件将重新创建.
If the route is changed, event when the new route adds the same component, the component is recreated.
首选解决方法是将模型保留在路由更改期间保持活动状态的共享服务中,并使用该服务中的数据恢复组件的先前状态.
The preferred workaround is to keep the model in a shared service that stays alive during route changes and use the data from this service to restore the previous state of the component.
有人提到,有计划支持路由器的自定义重用策略,但没有时间表,何时可用.
It was mentioned that there are plans to support custom reuse strategies with the router but no timeline when this will become available.
更新
对自定义重用策略的支持已添加到Angular2.
Support for custom reuse strategy was added to Angular2.
另请参见