0

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?

3
  • Try navigate using : routerLink="/routePathOfComponent2" routerDirection="forward" Commented Nov 12, 2019 at 2:37
  • 2
    You could pull any graph state and logic for the plotting (eg the interval) out of the component and into a singleton service. Inject the service into your component - so the component reflects the state from within the service. You can then freely navigate the application and when you return to the main page the data will be plotted. Commented Nov 12, 2019 at 3:06
  • If route needs not to change and still wants to navigate, you can use dynamic component loader and can create and destroy a component when you need. Commented Nov 12, 2019 at 4:44

1 Answer 1

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 },
Sign up to request clarification or add additional context in comments.

1 Comment

This is taking a sledgehammer to crack a nut.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.