【问题标题】:Ionic 3 storage saves for one page but not for the nextIonic 3 存储保存一页但不保存下一页
【发布时间】:2019-09-28 20:32:18
【问题描述】:

我创建了一个 Ionic 3 应用程序,将字符串存储在 Storage 中。我正在使用 SQLlite 插件,我可以看到该字符串已正确存储在页面上,因为在我 .set 值之后,我立即执行 .get 并能够验证键/值是否匹配。

this.storage.set('token', 'testing')
        .then(() => {
          this.storage.get('token')
            .then((token) => {
              console.log(token); //I get 'testing' as I should
            })
        });

当我在 Ionic 中转到下一页时,我尝试从存储中检索密钥,但我得到一个空值。

在下一页我有以下代码:

this.platform.ready()
.then(() => {
  this.storage.get('token')
  .catch(err => {
  })
  .then((token) => {
    console.log(token); //I get null here instead of 'testing'
  });
})

在我的 app.modules.ts 文件中,我在导入下添加了 import { IonicStorageModule } from '@ionic/storage';IonicStorageModule.forRoot()

【问题讨论】:

  • 我同意这个答案。如果需要,请使用 storage.ready() 但通常直接使用存储

标签: ionic3 ionic-storage


【解决方案1】:

this.platform.ready() 返回一个 Promise,所以它只解析或拒绝一次,我怀疑你在 set('token' 之前已经访问过这个页面,所以页面被添加到堆栈中(当你访问 Ionic 中的页面时,它会将它添加到堆栈,因此当您再次访问它时,它不需要再次初始化它)并且当您再次访问时,this.platform.ready() 不再触发。如果您需要检查平台,您可以:

if (this.platform.is('cordova')) { }

【讨论】:

  • 我同意这个答案;您可以使用this.platform.ready()app.component.ts 文件中检查平台是否准备就绪,然后将用户重定向到应用程序的第一页,然后直接使用存储,因为届时平台将准备就绪。
猜你喜欢
  • 2021-04-04
  • 1970-01-01
  • 2017-02-22
  • 1970-01-01
  • 1970-01-01
  • 2018-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多