2017-08-08 14 views
0

現在Ionicプロジェクトで作業しています。ユーザーのタイプに応じて別のページにリダイレクトする必要があります。残念ながら、this.nav.push()は動作していないようです。次のように私のコードは次のとおりです。Ionicのパラメータに応じて別のページにリダイレクト

export class Login { 

public email: string; 
public password: string; 
public nav: NavController; 

    constructor(public navCtrl: NavController) { 
} 

ionViewDidLoad() { 
console.log('ionViewDidLoad Login'); 
} 

userlogin() { 
Backendless.UserService.login(this.email, this.password, true) 
.then(this.userLoggedIn) 
.catch(this.gotError); 
} 

userLoggedIn(user) 
    { 
     console.log("user has logged in"); 
    if (user.user_type ==="g") 
    this.nav.push(Guesthome); 
    else 
    this.nav.push(Agenthome); 
    } 

    gotError(err) 
    { 
     console.log("error message - " + err.message); 
     console.log("error code - " + err.statusCode); 
    } 

} 
+0

コンテキストを維持するためにarrow functionを使うのか? – misha130

+0

エラー: 'エラーメッセージ - プロパティを読み取れません'、未定義のnavCtrl ' –

答えて

2

thisでコールバックを使用している間、あなたは元のコンテキストを維持する必要があることに留意してください。

オプション1:bindによってコンテキストを維持:

userlogin() { 
    Backendless.UserService.login(this.email, this.password, true) 
    .then(this.userLoggedIn.bind(this)) // <------ bind this to keep the context 
    .catch(this.gotError); 
} 

オプション2:エラーとは何

userlogin() { 
    Backendless.UserService.login(this.email, this.password, true) 
    .then(res => this.userLoggedIn(res))  
    .catch(this.gotError); 
} 
関連する問題