javascript - Ionic platform.exitApp() 不保存 localStorage

标签 javascript angular typescript ionic-framework ionic2

当我使用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() 延迟调用 exit。通过此超时,用户可以正确保存。

如何在不使用 setTimeout() 的情况下以正确的方式保留更改?

最佳答案

this.storage.set returns a promise, wait for it to resolve and do exit.

这是代码

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

关于javascript - Ionic platform.exitApp() 不保存 localStorage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52496402/

相关文章:

javascript - Bootstrap 选项卡面板没有删除事件类,但只是有时

angular - 如何使用 Sublime Text 3 获得 TypeScript 的支持?

typescript - Google Charts 是否有 TypeScript 定义文件?

javascript - 通过评估元素的数组子集

javascript - HTML 文本放入 Angular 的 $scope 属性中

javascript - 针对数字、字母或其他组的关键事件过滤

angular - 在 Angular 应用程序中代理时出现 504(网关超时)错误

typescript - TypeError : search. valueChanges.debounceTime 不是函数

typescript - 有没有办法在 TypeScript 的构造函数中隐式设置属性?

javascript - 在闭包上下文中抛出错误未到达 catch 子句