2017-08-08 76 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

什麼錯誤? – 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:使用arrow function保持上下文

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