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

时间:2017-05-29 07:25:59

标签: ionic2

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

 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});
            }

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

谢谢, 直径

2 个答案:

答案 0 :(得分: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.
            })
        }
    })

Loin 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});
            }

答案 1 :(得分:0)

您可以尝试以下方法:

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

登录后返回上一页。