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

查看:27
本文介绍了如何在 Angular 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.

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

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