首先加载 nativeStorage 时出现问题 运行

problem with load nativeStorage at first run

我需要帮助来解决这个问题。 在 home.ts 中,我检查用户之前是否使用 nativeStorage

登录过

home.ts

ionViewWillEnter() {
  this.load();
}

load() {

  this.chk = this.gettoken();

  alert(this.chk)
  if (this.chk == 'true')

  {
    //// code here
  }

  gettoken(): Promise<string> {
    this.nativeStorage.getItem('isLoggedIn').then((value) => {
      this.val = value;
    });
    return this.val;
  };

}

在登录页面中,我使用这个保存到存储:

this.nativeStorage.setItem('isLoggedIn','true');

第一次 运行 应用程序我得到了 'undefined' 现在可以了。在我成功登录并关闭应用程序并再次 运行 之后,我得到了 'undefined' 我必须转到另一个页面并再次返回主页才能获取存储值 (isLoggedIn) true

我把gettoken()改成这样:

async gettoken(){
    return await this.storage.getItem('isLoggedIn');
}   

但同样的问题

那么您也需要等待 .gettoken(),方法是:

async load(){
    this.chk = await this.gettoken();
    alert(this.chk);
}