Saurav Sircar
Saurav Sircar

Reputation: 187

Redirect to different page depending on parameter in Ionic

I'm currently working on an Ionic project wherein I need to redirect to different pages based on the type of user. Unfortunately, this.nav.push() doesn't seem to be working. My code is as follows:

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

}

Upvotes: 0

Views: 131

Answers (1)

Pengyy
Pengyy

Reputation: 38219

While using this in callbacks, keep in mind that you have to keep the original context.

Option1: keep the context by bind:

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

Option2: use arrow function to keep the context

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

Upvotes: 2

Related Questions