javascript - Ionic platform.exitApp() 不保存 localStorage
问题描述
当我使用我在执行退出之前所做platform.exitApp()
的更改时,localStorage
不会保存。
这是我启动它的方式:
this.user.profileGetbyId(this.user.userdata.id)
.subscribe(res => {
if(res && res.success) {
localStorage.setItem('user', res.data);
this.user.userdata = res.data;
console.log(this.user.userdata);
this.platform.exitApp();
//temp decision
// setTimeout(() => {
// this.platform.exitApp();
// }, 500);
}
})
这样,在exit()
启动之前控制台向我显示我已经更新了用户,但是当我下次启动应用程序时,我拥有之前的用户exit()
。
我尝试的下一件事是使用setTimeout()
. 有了这个timeout
,用户可以正确保存。
如何在没有 的情况下以正确的方式保持更改setTimeout()
?
解决方案
this.storage.set 返回一个承诺,等待它解决并退出。
这是代码
this.storage.set('user', res.data).then(data=>{
this.user.userdata = res.data;
this.platform.exitApp();
})
在 localStorage 中,在设置之前使用 JSON.stringify
localStorage.setItem("user", JSON.stringify(res.data));
要从 localStorage 中获取值,请使用
JSON.parse(localStorage.getItem("user"))
推荐阅读
- python - 是否可以将模型对象连接到 celery 任务?
- google-sheets - 导入范围查询:10秒以下的时长条件
- reactjs - 如何使用 jest 和酶导入具有模拟数据的手动模拟模块?
- matlab - 在 Matlab 中循环 Sortino 比率
- java - SocketException:JRE 1.6 上的连接重置,而不是 JRE 11
- blazor - Blazor MatMenu 在 foreach 循环中的所有菜单中取最后一个值
- css - 无法删除 CSS 属性:最大宽度:1200px
- javascript - 在我的 Angular 8 项目中集成谷歌地图时遇到一些问题
- reactjs - 更改反应选择中的选定值组件
- rest - 如何获取任何 REST API 的预期参数?