Angular RouteReuseStrategy

Angular 2/4 components are reloaded when the route is changed. To prevent this we can  implement the RouteReuseStrategy in our application to keep the state of the components while we jump around in our routes.

First we create our class that implements the RouteResuseStrategy. The first object we declare is “routesToCache” which contains the routes that we should reuse. We could cache all routes but that seems like waste of good memory.

Then we add the class as a provider in our module.

There you go… Have fun and reuse with caution 🙂

Leave a Comment

Your email address will not be published. Required fields are marked *