ionic 2 中的 localStorage 和 Storage 哪个更好?

localStorage and Storage in ionic 2 which is better?

我想将我的身份验证码存储在 localstorage 中,ionic2 提供了它自己的存储模块 ionic/storage

storage.set(key,value),
storage.get(key).then(value=> {
   console.log(value);
})

但我也尝试过这样存储,当我在浏览器中测试该应用程序时它有效。

localStorage.setItem(key, value);
localStorage.getItem('key');

任何人都可以向我解释哪种使用本地存储的方法更好吗?为什么?

Ionic 的存储绝对更好。正如您在 docs:

中看到的

Storage is an easy way to store key/value pairs and JSON objects. Storage uses a variety of storage engines underneath, picking the best one available depending on the platform.

When running in a native app context, Storage will prioritize using SQLite, as it's one of the most stable and widely used file-based databases, and avoids some of the pitfalls of things like localstorage and IndexedDB, such as the OS deciding to clear out such data in low disk-space situations.

When running in the web or as a Progressive Web App, Storage will attempt to use IndexedDB, WebSQL, and localstorage, in that order.

所以基本上,Ionic 的存储会尝试使用最佳可用选项,您甚至不必为此担心。在 docs 您还可以找到如何在您的项目中安装 SQLite,这将允许 Ionic 的存储使用它,并且将是在您的应用程序中存储信息的最佳方式。