使用Ionic2存储,我在自定义提供程序中完成了以下操作,我可以确认键值对已设置,我可以单独检索它们:
class Program
{
static void Main(string[] args) => Task.Run(() => MainAsync(args)).Wait();
static async Task MainAsync(string[] args)
{
var html = await GetResponseFromURI(new Uri("http://www.dailymail.co.uk/sciencetech/article-4408856/Samsung-building-flip-phone-TWO-screens.html?ITO=1490&ns_mchannel=rss&ns_campaign=1490"));
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
var nodes = doc.DocumentNode.SelectNodes("//div[@itemprop=\"articleBody\"]");
if (nodes != null)
{
Console.WriteLine(nodes.Select(node => node.InnerText).FirstOrDefault());
}
Console.ReadLine();
}
static async Task<string> GetResponseFromURI(Uri uri)
{
var response = "";
using (var client = new HttpClient())
{
HttpResponseMessage result = await client.GetAsync(uri);
if (result.IsSuccessStatusCode)
response = await result.Content.ReadAsStringAsync();
}
return response;
}
}
问题是我的一个页面中有一个函数需要访问HTTP请求的所有这三个值,所以我可以这样得到其中一个:
itemprop=\"articleBody\"
但是如何一次获得多个键值?我是否需要嵌套这些调用,或者是否有一种更简单的方法可以在一次调用中访问多个键,以便我可以执行以下操作:
this.storage.set('one', 1);
this.storage.set('two', 2);
this.storage.set('three', 3);
答案 0 :(得分:10)
我为此目的使用了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
});
答案 1 :(得分:3)
您可以按照以下所示进行操作。
this.storage.forEach( (value, key, index) => {
console.log("value", value);//store these values as you wish
console.log("key", key);
console.log("Index" index);
})
/**
* Iterate through each key,value pair.
* @param iteratorCallback a callback of the form (value, key, iterationNumber)
* @return Promise that resolves when the iteration has finished.
*/
forEach(iteratorCallback: (value: any, key: string, iterationNumber: Number) => any): Promise<null> {
return this._dbPromise.then(db => db.iterate(iteratorCallback));
}