I know lots have already asked this question but none of them gave clear answer. I just want to store the state of component when it navigates to another and then come back to previous one. I know we can save the data in service and then fetch the dats back to component. But, I think this is easy but not the better solution when the component is too complex. I read some router reuse things but non of them work in angular 2.
Asked
Active
Viewed 2.1k times
7
-
Can you provide the questions that don't provide a clear answers? This is a possible duplicate and have answered many times before – brijmcq Jul 24 '17 at 21:15
-
You can't save data in a component since when its destructed the data will go away – Sebastian Hernandez Jul 24 '17 at 21:15
1 Answers
7
There are several options:
1) Use a service. This is actually one of the best ways if the data is complex. I have a simple example here: https://blogs.msmvps.com/deborahk/build-a-simple-angular-service-to-share-data/
2) Routing parameters. You can specify required, optional, or query parameters. See this for more information: Sending data with route.navigate in Angular 2
If you are looking for information on the router reuse strategies, you can find some information here: https://medium.com/@juliapassynkova/angular-2-component-reuse-strategy-9f3ddfab23f5

DeborahK
- 57,520
- 12
- 104
- 129
-
Asked this question around July 2017 and now in 2018, what I understand is simply save the data in service file and again use that data when you comes back on that page. Hence other answer still didn't come from @angular team. – Sudhir Roy Jan 08 '18 at 20:51
-
1I don't believe that the Angular team monitors these forums. Stackoverflow is community supported. We are all just volunteers here. – DeborahK Jan 08 '18 at 22:19
-
1I updated my answer above to provide information on router reuse as well if that is what you are really looking for? – DeborahK Jan 08 '18 at 22:21
-
this is why I don't want to use Ionic4 (angular based routing) and keep Ionic3 (it keeps the component state automaticly!) – Ari Waisberg Nov 29 '19 at 12:22