I have two states A and B.I navigated from state A to state B.When i navigating back to the state A from state B the state A is loading again in angular.Does the State A destroyed while navigating from state A to state B?If State A destroyed,then how to prevent the state A loading again in back navigation
Asked
Active
Viewed 51 times
1
-
please look into this link - [https://stackoverflow.com/questions/41280471/how-to-implement-routereusestrategy-shoulddetach-for-specific-routes-in-angular](https://stackoverflow.com/questions/41280471/how-to-implement-routereusestrategy-shoulddetach-for-specific-routes-in-angular) – Basavaraj Bhusani Apr 24 '18 at 13:18
-
Please have a look at this.This worked for my requirement.https://stackoverflow.com/questions/44875644/custom-routereusestrategy-for-angulars-child-module/44876414#44876414 – Venkat Apr 27 '18 at 10:43
1 Answers
1
Yes, Angular holds only 1 state at a time. there are multiple ways to save state, having a service that holds private variables with getters and setters, saving it in localStorage/sessionStorage, query strings, etc... each of these has their own pros and cons, i would research a bit more about it on angular.io in the router section.
There are few classes that a component can "implement" for example OnInit and OnDestroy, then you would have those functions in the code so you would load/save state on each load/leave. Example:
export class ChatComponent implements OnInit, OnDestroy {
constructor(){}
onDestroy(){
//save logic goes here
}
onInit() {
//load logic goes here
}
}
Of course, you can load the state in constructor as well, but i find onInit to be better performance wise. But feel free to correct me.

StefanDimi
- 164
- 8