【问题标题】:Ionic storage GET离子存储 GET
【发布时间】:2020-12-05 21:22:29
【问题描述】:

我尝试从离子存储中获取价值,但在这里行不通。为什么 GET2 在 storage.get 之前执行?我的脑子坏了,求救。

  public storageGet(key: string){
    var uid = 0;
     this.storage.get(key).then((val) => {
      console.log('GET1: ' + key + ': ' + val);
      if (val != null) { uid = val;}
    });
    console.log('GET2: ' + key + ': ' + uid);
  return uid;
  }

返回:

GET2: uid: 0
GET1: uid: 1

【问题讨论】:

  • 这显然不是离子或角度的问题。这就是 Promise 的工作方式。

标签: angularjs typescript ionic-framework ionic4 ionic5


【解决方案1】:

您需要了解 Promise 的工作原理。

这段代码是异步的,then回调中的所有行都会被执行,但是你无法决定什么时候。

console.log("GET2")严格在storage.get之后执行,这部分是同步的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 2020-02-10
    • 2018-12-19
    • 2018-12-24
    相关资源
    最近更新 更多