Ionic 2返回上一页,重定向到登录/注册

问题描述 投票:0回答:2

我正在创建一个应用程序,我希望在用户尝试转到购物车页面时实现该应用程序,如果用户未登录,则将用户重定向回登录页面,成功登录后将用户重定向回源页面来了。但我无法这样做。在我尝试的代码下面,请让我知道如何执行此操作 - //如果用户登录则检查身份验证

 this.storage.get('authid').then((value) =>{
        this.apitoken = value;
        if(this.apitoken == '' || this.apitoken == undefined || this.apitoken == null){
            let emsg = this.toast.create({
                message:'Please try again',
                duration:2000
            })
            emsg.present();
            this.navCtrl.popToRoot();
            this.appCtrl.getRootNav().setRoot(Login); //Redirect to login page if not logged in
        }
        else{
            this.storage.get('deviceid').then((val) =>{
                this.dev_id = val;
                this.navCtrl.setRoot(CartPage); // go to cart page.
            })
        }
    })


 Login page - 
 if(data['code'] != 200){
                let errmsg = this.toast.create({
                    message:data['message'],
                    duration:2000
                });
                errmsg.present();
            }
            else{
                let smsg = this.toast.create({
                    message:data['message'],
                    duration:2000
                });
                smsg.present();
                this.storage.set('authid',data['data'].API_CURRENT_TOKEN);
         //Here I am redirecting to Menu page if dircetly click on loginpage but if it has redirected from any other page to login then it should go back to previous page(which is not happening). 
                this.navCtrl.push(Menu,{
                    userInfo:data['data'],
                    is_multiple: 2
                })
                .then(() => {         
                    const index = this.view.index;
                    this.navCtrl.remove(index);
                });
                // this.navCtrl.setRoot(ChangePassword,{'auth':data['data'].API_CURRENT_TOKEN});
            }

请帮助我摆脱这种情况。我的登录页面不是模态。

谢谢,他

ionic2
2个回答
0
投票

购物车页面

this.storage.get('authid').then((value) =>{
        this.apitoken = value;
        if(this.apitoken == '' || this.apitoken == undefined || this.apitoken == null){
            let emsg = this.toast.create({
                message:'Please try again',
                duration:2000
            })
            emsg.present();
            this.navCtrl.popToRoot();
            this.appCtrl.getRootNav().setRoot(LoginPage,{previousPage: "CartPage"}, {animate: true, direction: 'forward'}); //Redirect to login page if not logged in and Set Cart page name as previousPage parameter 
        }
        else{
            this.storage.get('deviceid').then((val) =>{
                this.dev_id = val;
                this.navCtrl.setRoot(CartPage); // go to cart page.
            })
        }
    })

登录页面

if(data['code'] != 200){
                let errmsg = this.toast.create({
                    message:data['message'],
                    duration:2000
                });
                errmsg.present();
            }
            else{
                let smsg = this.toast.create({
                    message:data['message'],
                    duration:2000
                });
                smsg.present();
                this.storage.set('authid',data['data'].API_CURRENT_TOKEN);
         //Here I am redirecting to Menu page if dircetly click on loginpage but if it has redirected from any other page to login then it should go back to previous page(which is not happening).

                if(navParams.get('previousPage') != "undefined" )
                {
                    this.navCtrl.push(Menu,{
                    userInfo:data['data'],
                    is_multiple: 2
                      })
                    .then(() => {         
                      const index = this.view.index;
                      this.navCtrl.remove(index);
                     });
                }else
                {
                 //Here you get CartPage Name
                   this.navCtrl.push(navParams.get('previousPage'),{
                    userInfo:data['data'],
                    is_multiple: 2
                      });
                 }


                // this.navCtrl.setRoot(ChangePassword,{'auth':data['data'].API_CURRENT_TOKEN});
            }

0
投票

你可以试试这个:

this.navCtrl.push(this.navCtrl.getPrevious().name);

登录后返回上一页。

© www.soinside.com 2019 - 2024. All rights reserved.