要防止在Angular 2中进行布线时重新创建组件 [英] Want to prevent Component recreation while routing in Angular 2

查看:84
本文介绍了要防止在Angular 2中进行布线时重新创建组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有两条路线DashboardProfile. 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.

另请参见

查看全文

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