2017-06-06 90 views
0

我想實現註銷功能。我正在使用FB身份驗證。我的問題在於我成功註銷後(這發生在側面菜單中)我返回登錄屏幕。但現在,如果我點擊設備的背面按鈕,它帶我到首頁(不知道如何避免這種情況?離子2側菜單和註銷和返回鍵導航

這是我app.component.ts

import { Component, ViewChild } from '@angular/core'; 
import { Platform, Nav, AlertController } from 'ionic-angular'; 
import { StatusBar } from '@ionic-native/status-bar'; 
import { SplashScreen } from '@ionic-native/splash-screen'; 
import { LoginPage } from '../pages/login/login'; 
import { NativeStorage } from '@ionic-native/native-storage'; 
import { TabsPage } from '../pages/tabs/tabs'; 
import { Facebook } from 'ionic-native'; 
@Component({ 
    templateUrl: 'app.html', 

}) 
export class MyApp { 
    @ViewChild(Nav) nav: Nav; 
    rootPage: any = LoginPage; 

    constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, private nativeStorage: NativeStorage, private alertCtrl: AlertController) { 
    platform.ready().then(() => platform.ready().then(() => { 
     this.nativeStorage.getItem("userId").then((data) => { 
     console.log(data.userExists); 
     this.rootPage = TabsPage; 
     this.nav.push(TabsPage); 

     }, (error) => { 
     console.log("No data in storage"); 
     this.nav.push(LoginPage); 
     }) 
     statusBar.styleDefault(); 
     splashScreen.hide(); 
    }) 

    ) 
    } 

    presentConfirm() { 
    let alert = this.alertCtrl.create({ 
     title: "Confirm Logout", 
     message: "Do you really really want to quit this awesome app?", 
     buttons: [{ 
     text: 'Nah! Just Kidding!', role: 'cancel', handler:() => { 
     } 
     }, 
     { 
     text: "Ok", 
     handler:() => { 
      this.nativeStorage.clear(); 
      this.nav.push(LoginPage);   
      this.rootPage = LoginPage; 
      Facebook.logout().then((response) => { 
      }, (error) => { 
      }) 
     } 
     }] 
    }); 
    alert.present(); 
    } 

} 

這是我的登錄頁面。 Login.ts

import { Component } from '@angular/core'; 
import { NavController } from 'ionic-angular'; 
import { Facebook } from 'ionic-native'; 
import { NativeStorage } from '@ionic-native/native-storage'; 
import { TabsPage } from '../tabs/tabs' 

@Component({ 
    selector: 'login-home', 
    templateUrl: 'login.html' 
}) 
export class LoginPage { 

    constructor(public navCtrl: NavController, private nativeStorage: NativeStorage) { 

    } 

    FBLogin() { 
    //this.navCtrl.push(TabsPage); - To be used on browser environment 
    Facebook.login(['email']).then((response) => { 
     Facebook.getLoginStatus().then((response) => { 
     if (response.status == 'connected') { 
      this.navCtrl.push(TabsPage); // - to be used on device 
      console.log("Setting Storage") 
      this.nativeStorage.setItem("userId", { userExists: true });   
      Facebook.api('/' + response.authResponse.userID + '?fields=id,name,gender', []).then((response) => { 
      }, (error) => { 
      alert(error) 
      }) 
     } 
     else 
      alert("Not Logged in"); 
     }) 

    }, (error) => { 
     console.log((error)); 
    }) 
    } 


} 

這是我的菜單

<ion-menu swipeEnabled="false" side="right" [content]="content"> 
    <ion-toolbar> 
    <ion-title>Menu</ion-title> 
    </ion-toolbar> 
    <ion-content> 
    <ion-list>  
    <p>&nbsp;</p> 
     <button menuClose padding-top ion-item> Test 1 </button>  
     <button menuClose ion-item> Test 2 </button> 
     <button menuClose ion-item (click)="presentConfirm()"><ion-icon name="power"></ion-icon>&nbsp;&nbsp;Logout </button> 
    </ion-list> 
    </ion-content> 
</ion-menu> 
<ion-nav id="nav" [root]="rootPage" #content swipe-back-enabled="false"></ion-nav> 

回答

2

你的問題是,你正在做的:

 this.nav.push(LoginPage);   

在您的註銷處理程序中。 這意味着LoginPage被添加到您的舊導航堆棧頂部。您需要設置一個新的堆棧並將root設置爲LoginPage。 做:

this.nav.setRoot(LoginPage); 
+0

謝謝噸,它的作品! –

+0

很高興聽到它:) –