首页 > 解决方案 > 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()

标签: javascriptangulartypescriptionic-frameworkionic2

解决方案


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"))

推荐阅读