【问题标题】:ionic 3 alert button handler async issueionic 3 警报按钮处理程序异步问题
【发布时间】:2018-12-14 22:25:09
【问题描述】:

我目前正在对来自 http.post 调用的错误应用代码。

this.http.post('api here', {'email':this.username})
.subscribe(response => {
    if(response === true){
        localStorage.setItem('Username', this.username);
        this.navCtrl.push(Page_1);
},
(error) => {
    if(error == '404'){
        let alert = this.alertCtrl.create({
            title:'',
            message:'',
            buttons: [
                 {
                     text:'no',
                     handler: () => {
                         console.log('no clicked');
                     }
                 },
                 {
                     test:'yes',
                     handler: () => {
                         localStorage.setItem('Register_User', 
                         this.username);
                         this.navCtrl.push(Page_2);
                     }
                 }
                    ]
         });
         alert.present();
     }}
})

所以我上面遇到的问题是,当我从服务器收到 404 错误并且我想将它们定向到第 2 页时,它没有设置本地存储 register_username。我的猜测是,这是一个异步的东西,它会在设置存储之前推送到页面。我该如何解决这个问题?

【问题讨论】:

  • 然后超时推送到下一页,保存在本地存储中,然后转到页面setTimeout(() => { this.navCtrl.push(Page_2); },1500);
  • 你在设置数据后推送页面做得很好,localstorage不是异步的,我认为问题在于你对http post的处理。

标签: http asynchronous ionic-framework ionic3 local-storage


【解决方案1】:

如果localStorage.setItem承诺,那么

handler: () => {
  localStorage.setItem('Register_User', this.username)
    .then(()=>{this.navCtrl.push(Page_2);})
}

【讨论】:

    猜你喜欢
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多