在角度5重新加载后如何保留服务数据 [英] How to persist service data after reload in angular 5

查看:79
本文介绍了在角度5重新加载后如何保留服务数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项在组件之间传递数据的服务,当它被调用时它可以工作,但是当我重新加载服务数据时就重置了.任何客户端存​​储?我很好奇的一件事是我可以使用rxjs来实现这一点,但我不确定这将如何帮助我害怕使用localstorage,因为数据将主要是对象或数组而不是用户ID或会话ID.真的很有帮助.我提供了一个示例示例来帮助我.您可以在下面找到

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

推荐答案

您应该使用 localstorage/sessionstorage 或组件之间的共享服务.

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

您可以使用JSON.stringify存储信息(对象/数组).如果您使用的共享服务变成单例,则可以跨任何组件进行访问.

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.

这篇关于在角度5重新加载后如何保留服务数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆