2017-05-29 49 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

相關問題