首页 > 解决方案 > 离子存储 GET

问题描述

我尝试从离子存储中获取价值,但在这里不起作用。为什么 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

标签: angularjstypescriptionic-frameworkionic4ionic5

解决方案


你需要了解 Promise 是如何工作的。

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

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


推荐阅读