Avoid destroying component instance when routing to another component

可紊 提交于 2020-02-06 08:18:06

问题


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.

Is it possible?


回答1:


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

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

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


来源:https://stackoverflow.com/questions/58811111/avoid-destroying-component-instance-when-routing-to-another-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!