使用 Ionic2 存储,我在自定义提供程序中完成了以下操作,我可以确认键值对已设置并且我可以单独检索它们:
this.storage.set('one', 1);
this.storage.set('two', 2);
this.storage.set('three', 3);
问题是我的一个页面中有一个函数需要为 HTTP 请求访问所有这三个值,因此我可以像这样获取其中一个值:
this.storage.get('one').then((value) => {
// Preform http request sending value in POST data
});
但是我如何一次获得多个键值呢?我是否需要嵌套这些调用,或者是否有一种更简单的方法可以在一次调用中访问多个键,以便我可以执行以下操作:
this.storage.get(['one', 'two', 'three']).then((values) => {
console.log(values.one); // I dunno, something like this
});
最佳答案
我为此目的使用了 Promise.all
Promise.all([this.storage.get("email"), this.storage.get("username")]).then(values => {
console.log("email", values[0]);
console.log("username", values[1]);
// every other thing can happen here e.g call a method
});
关于angular - 从 Ionic2 存储中获取多个键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43395691/