问题
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