typescript Ionic 3 的适当本地存储
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46629594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Appropriate Local Storage for Ionic 3
提问by giorgionasis
I am reading about Local Storage and I am quite confused. As I see there are two options:
我正在阅读有关本地存储的信息,但我很困惑。如我所见,有两种选择:
Native Storage , import { NativeStorage } Ionic Storage, import { IonicStorageModule } My app is developed with Ionic 3 and I am trying to save an array of object localy after retrieve it from Parse Server.
Native Storage , import { NativeStorage } Ionic Storage, import { IonicStorageModule } 我的应用程序是用 Ionic 3 开发的,我试图在从 Parse Server 检索对象数组后将其保存在本地。
With Ionic 1 I stored the objects array like this:
使用 Ionic 1,我像这样存储了对象数组:
setUsers (users){
window.localStorage.users_data = JSON.stringify(users);
}
getUsers(){
return JSON.parse(window.localStorage.users_data || '[]');
}
So now, what is the best option to save my data and stringfy them and parse them?
那么现在,保存我的数据并将它们字符串化并解析它们的最佳选择是什么?
Native storage or Ionic Storage?
原生存储还是离子存储?
Thank you a lot
非常感谢
回答by Sampath
You can easily use Ionicstoragemodule here. You just need to install SQLite
plugin. Details are in the above doc. Then you can use it natively on iOS
and Android
too without any issue.
您可以在此处轻松使用Ionic存储模块。你只需要安装 SQLite
插件。详细信息在上面的文档中。然后你可以在本地使用它iOS
,Android
也没有任何问题。
Usage:
用法:
// set a key/value
storage.set('name', 'Max');
// Or to get a key/value pair
storage.get('age').then((val) => {
console.log('Your age is', val);
});
回答by Tariq Saeed
Nowadays I work with ionic 4 and I have experienced unpleasant latency of the @ionic/storagemodule, but after I switched to the @ionic-native/native-storageI got a better performance. And by the way you still can use it while testing with the browser using the ionic cordova run browsercommand not the ionic servecommand.
现在我使用 ionic 4 并且我经历了@ionic/storage模块令人不快的延迟,但是在我切换到@ionic-native/native-storage 之后,我获得了更好的性能。顺便说一下,在使用ionic cordova run browser命令而不是ionic serve命令对浏览器进行测试时,您仍然可以使用它。
So what I am trying to say that native pluginsalways win in the matter of performance.
所以我想说的是,本地插件总是在性能方面获胜。
Best Wishes
最好的祝愿