Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we save state of component in angular 2?

Tags:

angular

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.

like image 916
Sudhir Roy Avatar asked Jul 24 '17 21:07

Sudhir Roy


1 Answers

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

like image 103
DeborahK Avatar answered Oct 05 '22 04:10

DeborahK