【发布时间】: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