本地持久存储 [英] Local persistent storage
问题描述
我有一个流星反应应用程序,我需要一种方式来保存一些信息本地,这也将是持久的。例如,如果一个用户保存一个json,我想使用同一个json,即使该应用程序被关闭并重新打开。我尝试了groundDb,但它需要服务器端。我需要这个功能,以使每个用户保存信息,如游戏水平。这将是巨大的,如果我可以使用它在网络上,以及不只是为本机版本。谢谢!
I have a meteor react app and I need a way to save some info locally which will also be persistent. For example, if a user save a json, I want to use that same json even if the app is closed and reopen later. I tried groundDb but it requires server side as well. I need this feature to enable each user to save info such as game level. It will be great if I can use it on the web as well and not just for native versions. Thanks!
推荐答案
要加入职员,以下是正确的解释:
To join the pedants, here's the proper explanation:
您应该首先使用全局JSON对象及其stringify方法将对象字面量转换为JSON:
You should first convert your object literal into JSON using the global JSON object and its stringify method:
let data = {a: 'some', b: 'data', c: null};
let json = JSON.stringify(data);
localStorage.setItem('data', json);
当您要检索数据并在应用程序中使用它时,您需要解析JSON返回对象常量:
When you want to retrieve the data and use it in your application, you'll need to parse the JSON back into an object literal:
let json = localStorage.getItem('data');
let data = JSON.parse(json);
这篇关于本地持久存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!