路由到另一个组件时避免破坏组件实例 [英] Avoid destroying component instance when routing to another component

查看:64
本文介绍了路由到另一个组件时避免破坏组件实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由2个组件组成的Angular应用程序.最主要的是一个图形,该图形绘制了json文件中的数据.每5s间隔绘制一个新点.

I have an Angular application which consists of 2 components. The main one is a graph which plots data from a json file. Each 5s interval a new point is plotted.

我的问题是,当我路由显示第二个组件然后返回到主要组件时,所有已经绘制的数据都将被清除,并且该组件会从头开始.

My problem is that when I route to display the second component and then go back to the main one, all the data already plotted is cleared and the component starts from the begining again.

我已经在互联网上进行了研究,但没有明确的答案.基本上,我希望避免在布线时破坏主要组件,因此即使该组件当前未显示,它也可以继续绘制点(或至少具有相同的先前数量).

I have researched on the internet but I don't get a clear answer. Basically I want to avoid the main component to destroy on routing so It can continue ploting points (or at least have the same previous amount) even if the component is not displayed at the moment.

有可能吗?

推荐答案

您应该创建自己的实现 RouteReuseStrategy .例如,您可以查看 AppRouteReuseStrategy .

You should create your own class that implements RouteReuseStrategy. As an example you may have a look at AppRouteReuseStrategy.

请注意,该课程需要在app.module.ts中注册为提供者.

Please note that the class needs to be registered as a provider in app.module.ts.

providers: [
    { provide: RouteReuseStrategy, useClass: AppRouteReuseStrategy },

这篇关于路由到另一个组件时避免破坏组件实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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