Ray
Ray

Reputation: 123

Avoid destroying component instance when routing to another component

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?

Upvotes: 0

Views: 1183

Answers (1)

uminder
uminder

Reputation: 26150

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 },

Upvotes: 1

Related Questions