Hi, I am creating a login page that if the user login before. It will directly redirect to user detail page.
But the code like the following is not work.
constructor(private nav: NavController,private global :Global) {
this.local = new Storage(LocalStorage);
this.currentUser = this.local.get('userid');
this.local.get('userid').then((value) => {
var dt = value
console.log("Local Storage value:", dt)
if(dt !=null)
this.nav.setRoot(UserdetailPage)
})
}
it is work if you normally login, it will direct to user detail page.
loginSucessAlert(){
this.currentUser = Parse.User.current().id;
this.local.set('userid',this.currentUser);
this.global.setUserId(Parse.User.current().id);
this.global.setUserName(this.username);
let alert = Alert.create({
title:'Login',
subTitle:'Hi '+this.username+', Press to Continue.',
buttons: [{
text: 'Ok',
handler: () => {
// user has clicked the alert button
// begin the alert's dimiss transition
let navTransition = alert.dismiss();
navTransition.then(() => {
// here comes your navigation action (push, pop, setRoot)
this.nav.setRoot(UserdetailPage);
});
return false;
}
}]
});
this.nav.present(alert);
}
If you are referencing an object in a template whose value will eventually be set by an asynchronous operation (as it looks like you are doing), you have a few options: initialize it before starting the asynchronous operation with a valid dummy, guard all access to it in the template with checks for null, the Elvis operator, or the AsyncPipe, or ensure using ngIf that it never gets instantiated until the value is available.
why not just use this.nav.push(UserDetailPage), rootPage is the starting page of the app, there is just one starting page and one starting moment - if I said that correctly. Meaning rootPage is supposed to be determined in app.ts not any other sub-compoennts.