根据Ionic中的参数重定向到不同的页面

时间:2017-08-08 06:23:14

标签: angular typescript ionic-framework

我目前正致力于一个Ionic项目,我需要根据用户类型重定向到不同的页面。不幸的是,this.nav.push()似乎无法正常工作。我的代码如下:

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 );
    }

}

1 个答案:

答案 0 :(得分: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 );
}