How to persist service data after reload in angular 5

爷,独闯天下 提交于 2020-07-18 14:28:49

问题


I have a service to pass data between components and when called it works but when i reload the service data gets reset.So when i searched on reload the memory is reset so is there a way to persist data even after reload without using any client side storage ? One thing i am curious is that can i achieve this using rxjs but i am not sure how this would help ?I fear to use localstorage because the data will be mostly objects or array and not user id or session id.Any inputs on this would be really helpful.I have produced a sample example to help me with.You can find below

https://stackblitz.com/edit/component-data-sharing-service


回答1:


You should be using localstorage/sessionstorage or shared service among components.

You can store the information(object/arrays) using JSON.stringify. If you are using shared service which becomes singleton so you can access across any components.




回答2:


What do you mean "without using client side storage"? You have to use client side storage to persist data in client side...

Make a singleton shared service in which you can save your data.

Like so:

https://octoperf.com/blog/2016/03/29/singleton-service-using-angular2/



来源:https://stackoverflow.com/questions/48473378/how-to-persist-service-data-after-reload-in-angular-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!