2017-05-29 48 views
0

我正在创建一个应用程序,如果用户尝试转到购物车页面并且用户未登录,用户返回到登录页面,成功登录后将用户重定向到源页面,但是我无法这样做。 下面的代码段,我想请让我知道如何做到这一点 - //检查认证,如果用户在Ionic 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}); 
      } 

登录请帮我摆脱这种situation.My登录页面的不一种模式。

感谢, 迪亚

回答

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

我已经尝试过这种方式,但抛出的错误 未捕获的(在承诺):无效的链接:CartPage –

+0

'从离子角'导入{IonicPage};'然后使用注释'@IonicPage(名称:“CartPage” ') – hrdkisback

相关问题