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

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

问题描述

我有一个由 2 个组件组成的 Angular 应用程序.主要的是一个图表,它绘制来自 json 文件的数据.每 5 秒间隔绘制一个新点.

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天全站免登陆